gh-pages / com.nextfaze.devfun.inject / InstanceProvider
InstanceProvider
interface InstanceProvider
(source)
Provides object instances for one or more types.
A rudimentary form of dependency injection is used throughout all of DevFun (not just for user-code function
invocation, but also between modules, definition and item processing, and anywhere else an object of some type is
desired - in general nothing in DevFun is static (except for the occasional object
, but even then that is usually
an implementation and uses DI).
This process is facilitated by various instance providers - most of which is described at the wiki entry on Dependency Injection.
To quickly and simply provide a single object type, use captureInstance or singletonInstance, which creates a
CapturingInstanceProvider that can be added to the root (composite) instance provider at DevFun.instanceProviders
.
e.g.
class SomeType : BaseType
val provider = captureInstance { someObject.someType } // triggers for SomeType or BaseType
val singleInstance = singletonInstance { SomeType() } // triggers for SomeType or BaseType (result of invocation is saved)
If you want to reduce the type range then specify its base type manually:
val provider = captureInstance<BaseType> { someObject.someType } // triggers only for BaseType
Be aware of leaks! The lambda could implicitly hold a local this
reference.
See Also
Functions
Name | Summary |
---|---|
get | abstract operator fun <T : Any > get(clazz: KClass <out T >): T ? Try to get an instance of some clazz. |
Inheritors
Name | Summary |
---|---|
AndroidInstanceProviderInternal | interface AndroidInstanceProviderInternal : InstanceProvider |
CapturingInstanceProvider | class CapturingInstanceProvider<out T : Any > : InstanceProvider An instance provider that requests an instance of a class from a captured lambda. |
ConstructingInstanceProvider | class ConstructingInstanceProvider : InstanceProvider Provides objects via instance construction. Type must be annotated with Constructable. |
Dagger2InstanceProvider | abstract class Dagger2InstanceProvider : InstanceProvider |
KObjectInstanceProvider | class KObjectInstanceProvider : InstanceProvider Handles Kotlin object and companion object types. |
ThrowingInstanceProvider | interface ThrowingInstanceProvider : InstanceProvider Same as InstanceProvider, but throws ClassInstanceNotFoundException instead of returning null . |