Context

open class Context : Disposable

Handles dependency injection mechanism. Allows binding selected classes with their providers.

Note that Context instances are not considered fully thread-safe - while it is usually safe to access a fully built context from within multiple threads, you should override createProvidersMap method and return a thread-safe MutableMap implementation to avoid concurrency bugs.

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
inline fun <Type : Any> bind()

Automatically registers a provider for Type that will use reflection to create a new instance each time it is requested. All required constructor parameters will be extracted from Context. Note that the provider might throw InjectionException if any of the dependencies are missing, or com.badlogic.gdx.utils.reflect.ReflectionException when unable to construct an instance.

inline fun <Type : Any> bind(noinline provider: () -> Type)

Allows to bind a provider producing instances of the selected type.

fun <Type : Any> bind(vararg to: KClass<out Type>, provider: () -> Type)

Allows to bind a provider to multiple classes in hierarchy of the provided instances class.

Link copied to clipboard
inline fun <Type : Any> bindSingleton(): Type

Automatically creates and registers an instance of Type with reflection. All required constructor parameters will be extracted from Context and must be present before calling this method.

inline fun <Type : Any> bindSingleton(singleton: Type)
inline fun <Type : Any> bindSingleton(provider: () -> Type)

Allows to bind a singleton to the chosen class.

fun <Type : Any> bindSingleton(vararg to: KClass<out Type>, singleton: Type)

Allows to bind a singleton instance to multiple classes in its hierarchy.

inline fun <Type : Any> bindSingleton(vararg to: KClass<out Type>, provider: () -> Type)

Allows to bind the result of the provider to multiple classes in its hierarchy.

Link copied to clipboard
open fun clear()

Removes all user-defined providers and singletons from the context. Context itselfs will still be present and injectable with provider and inject.

Link copied to clipboard
inline fun <Type : Any> contains(): Boolean
operator fun contains(type: Class<*>): Boolean
operator fun contains(type: KClass<*>): Boolean
Link copied to clipboard
open override fun dispose()

Disposes of all Disposable singletons and providers registered in the context and removes them. Note that if registered provider provides Disposable instances, but it does not track the resources and does not implement Disposable itself, the provided assets will not be disposed by this method. Context does not track all injected assets: only directly registered objects are disposed. clear is called after all assets are disposed. Errors are caught and logged.

Link copied to clipboard
open fun <Type> getProvider(forClass: Class<Type>): () -> Type

Extracts provider of instances of the selected type. This method is internally used by inlined injection methods.

Link copied to clipboard
inline fun <Type : Any> inject(): Type

Provides instance of the selected type.

Link copied to clipboard
inline operator fun <Type : Any> invoke(): Type

Provides instance of the selected type. Utility method allowing to call context as a function.

Link copied to clipboard
inline fun <Type : Any> newInstanceOf(): Type

Constructs an instance of Type using reflection. All required constructor parameters will be extracted from Context and must be present before calling this method.

Link copied to clipboard
inline fun <Type : Any> provider(): () -> Type

Extracts provider of instances of the selected type.

Link copied to clipboard
inline fun Context.register(init: Context.() -> Unit): Context

Allows to register new components in the context with builder-like DSL.

Link copied to clipboard
inline fun <Type : Any> remove(): () -> Type?

Removes singleton or provider of instances of the selected type registered in the Context.

Link copied to clipboard
open fun <Type> removeProvider(ofClass: Class<Type>): () -> Type?

Removes provider of instances of the selected type. This method is internally used by inlined removal methods.

Link copied to clipboard
open fun setProvider(forClass: Class<*>, provider: () -> Any)

Adds the selected provider to the Context. This method is internally used by inlined binding methods.