winter / io.jentz.winter / Injector

Injector

class Injector

The Injector provides a was to inject dependencies into properties via Kotlins property delegates.

E.g.

class MyClass {
  private val injector = Injector()
  private val eagerService: Service1 by injector.instance()
  private val lazyService: Service2 by injector.lazyInstance()
  private val fromFactory: Widget by injector.instance(Color.BLUE)

  fun onCreate() {
    injector.inject(Application.graph)
  }

}

Types

InjectedProperty

abstract class InjectedProperty<out T> : ReadOnlyProperty<Any?, T>

Constructors

<init>

Injector()

The Injector provides a was to inject dependencies into properties via Kotlins property delegates.

Properties

injected

val injected: Boolean

Functions

factory

fun <A, R> factory(qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<(A) -> R>

Creates and registers a property delegate for a factory of type (A) -> R.

factoryOrNull

fun <A, R> factoryOrNull(qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<((A) -> R)?>

Creates and registers a property delegate for an optional factory of type (A) -> R.

inject

fun inject(graph: Graph): Unit

Connects the given graph with all previously created property delegates to resolve the dependencies.

instance

fun <T : Any> instance(qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<T>

Creates and registers a property delegate for an instance of type T.

fun <A, R : Any> instance(argument: A, qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<R>

Creates and registers a property delegate for an instance of type R. This lookups a factory of type (A) -> R and calls it with the supplied argument.

instanceOrNull

fun <T : Any> instanceOrNull(qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<T?>

Creates and registers a property delegate for an optional instance of type T.

fun <A, R : Any> instanceOrNull(argument: A, qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<R?>

Creates and registers a property delegate for an optional instance of type R. This lookups a factory of type (A) -> R and calls it with the supplied argument or returns null if no factory of type (A) -> R exists.

instancesOfType

fun <T : Any> instancesOfType(generics: Boolean = false): Injector.InjectedProperty<Set<T>>

Creates and registers a property delegate for a Set of instances of type T.

lazyInstance

fun <T : Any> lazyInstance(qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<T>

Creates and registers a lazy property delegate for an instance of type T.

fun <A : Any, R : Any> lazyInstance(argument: A, qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<R>

Creates and registers a lazy property delegate for an instance of type R.

lazyInstanceOrNull

fun <T : Any> lazyInstanceOrNull(qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<T?>

Creates and registers a lazy property delegate for an optional instance of type T.

fun <A : Any, R : Any> lazyInstanceOrNull(argument: A, qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<R?>

Creates and registers a lazy property delegate for an optional instance of type R.

lazyInstancesOfType

fun <T : Any> lazyInstancesOfType(generics: Boolean = false): Injector.InjectedProperty<Set<T>>

Creates and registers a lazy property delegate for a Set of instances of type T.

provider

fun <T : Any> provider(qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<Provider<T>>

Creates and registers a property delegate for a Provider of type () -> T.

fun <A, R : Any> provider(argument: A, qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<Provider<R>>

Creates and registers a property delegate for a Provider of type () -> R. This lookups a factory of type (A) -> R and calls it with the supplied argument when the provider gets invoked.

providerOrNull

fun <T : Any> providerOrNull(qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<Provider<T>?>

Creates and registers a property delegate for an optional Provider of type T.

fun <A, R : Any> providerOrNull(argument: A, qualifier: Any? = null, generics: Boolean = false): Injector.InjectedProperty<Provider<R>?>

Creates and registers a property delegate for a Provider of type () -> R. This lookups a factory of type (A) -> R and calls it with the supplied argument when the provider gets invoked. The delegate will return null when no factory of type (A) -> R exists.

providersOfType

fun <T : Any> providersOfType(generics: Boolean = false): Injector.InjectedProperty<Set<Provider<T>>>

Creates and registers a property delegate for a Set of providers of type T.