DependencyRegistry

A central registry for managing and resolving dependencies within a dependency injection context.

The DependencyRegistry class acts as both a DependencyProvider and a DependencyResolver. It facilitates the registration of dependencies through a DependencyProvider as well as the resolution and validation of dependencies using the provided resolver mechanism. This registry also supports reflective creation of instances and type-safe access to registered dependencies.

Report a problem

Parameters

resolver

The delegate resolver for finding registered dependencies.

provider

The delegate provider responsible for registering new dependencies..

reflection

A reflection implementation that supports dynamic instantiation of classes.

Constructors

Link copied to clipboard
constructor(resolver: DependencyResolver, provider: DependencyProvider)

Types

Link copied to clipboard
inner class KeyContext<T>(val key: DependencyKey)

DSL class for performing multiple actions for the given key and type.

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open override val reflection: DependencyReflection
Link copied to clipboard

Functions

Link copied to clipboard
fun cleanup(key: DependencyKey, cleanup: (Any?) -> Unit)
inline fun <T> cleanup(name: String? = null, noinline cleanup: (T) -> Unit): DependencyRegistry.KeyContext<T>
Link copied to clipboard
open override fun contains(key: DependencyKey): Boolean

Checks if the given dependency key is present in the dependency map.

Link copied to clipboard
inline suspend fun <T : Any> DependencyResolver.create(): T

Creates an instance of the specified type T using the dependency resolver. This function uses the DependencyReflection mechanism to dynamically construct an instance of the requested type, resolving dependencies as needed.

inline suspend fun <T : Any> DependencyResolver.create(kClass: KClass<out T>): T

Creates or retrieves an instance of the specified type T from the DependencyResolver. If the instance does not already exist, it is created using reflection.

Link copied to clipboard
open suspend override fun <T> get(key: DependencyKey): T

Retrieves an instance of the dependency associated with the given key from the dependency map.

Link copied to clipboard

Get an item from the dependency map synchronously.

Blocking is unavailable for other platforms, so we instead attempt to get the completed value.

Link copied to clipboard
open override fun <T> getDeferred(key: DependencyKey): Deferred<T>
Link copied to clipboard
Link copied to clipboard
open suspend override fun <T> getOrPut(key: DependencyKey, defaultValue: suspend () -> T): T

Retrieves the value associated with the specified key if it exists. If the key does not already have an associated value, the result of invoking the defaultValue function will be stored and returned as the value for the given key.

Link copied to clipboard
inline fun <T> key(name: String? = null, noinline handler: DependencyRegistry.KeyContext<T>.() -> Unit): DependencyRegistry.KeyContext<T>

Creates a new KeyContext for the specified type T and an optional name. The given handler is invoked on the created KeyContext, allowing configuration such as defining a provider or cleanup logic for the dependency.

Link copied to clipboard
open override fun named(key: String): DependencyResolverContext

Decorates the dependency resolver with a qualified name for the expected type.

Link copied to clipboard

Combines two DependencyMaps into one.

Link copied to clipboard
inline fun <T : Any> provide(kClass: KClass<out T>): DependencyRegistry.KeyContext<T>

Provides an instance of the dependency associated with the specified kClass.

inline fun <T> provide(name: String? = null, noinline provide: suspend DependencyResolver.() -> T?): DependencyRegistry.KeyContext<T>

Basic call for providing a dependency, like provide<Service> { ServiceImpl() }.

Link copied to clipboard
inline fun <E> DependencyRegistry.provide(crossinline function: suspend () -> E): DependencyRegistry.KeyContext<E>

Registers a dependency provider that takes no parameters and returns a value of type E.

inline fun <E, I1> DependencyRegistry.provide(crossinline function: suspend (I1) -> E): DependencyRegistry.KeyContext<E>

Registers a dependency provider that takes one input parameter and returns a value of type E.

inline fun <E, I1, I2> DependencyRegistry.provide(crossinline function: suspend (I1, I2) -> E): DependencyRegistry.KeyContext<E>

Registers a dependency provider that takes two input parameters and returns a value of type E.

inline fun <E, I1, I2, I3> DependencyRegistry.provide(crossinline function: suspend (I1, I2, I3) -> E): DependencyRegistry.KeyContext<E>

Registers a dependency provider that takes three input parameters and returns a value of type E.

inline fun <E, I1, I2, I3, I4> DependencyRegistry.provide(crossinline function: suspend (I1, I2, I3, I4) -> E): DependencyRegistry.KeyContext<E>

Registers a dependency provider that takes four input parameters and returns a value of type E.

inline fun <E, I1, I2, I3, I4, I5> DependencyRegistry.provide(crossinline function: suspend (I1, I2, I3, I4, I5) -> E): DependencyRegistry.KeyContext<E>

Registers a dependency provider that takes five input parameters and returns a value of type E.

inline fun <E, I1, I2, I3, I4, I5, I6> DependencyRegistry.provide(crossinline function: suspend (I1, I2, I3, I4, I5, I6) -> E): DependencyRegistry.KeyContext<E>

Registers a dependency provider that takes six input parameters and returns a value of type E.

inline fun <T> DependencyProvider.provide(name: String? = null, noinline provide: DependencyResolver.() -> T?)

Basic call for providing a dependency, like provide<Service> { ServiceImpl() }.

Link copied to clipboard
inline operator fun <T> provideDelegate(thisRef: Any?, prop: KProperty<*>): ReadOnlyProperty<Any?, T>

Provides a delegated property for accessing a dependency from a DependencyRegistry. This operator function allows property delegation, ensuring the required dependency is registered and retrievable through the registry.

Link copied to clipboard

Indicates that the given dependency is required.

Link copied to clipboard
inline suspend fun <T> resolve(key: String? = null): T

Get the dependency from the map for the key represented by the type (and optionally, with the given name).

Link copied to clipboard
inline suspend fun <T> DependencyResolver.resolve(key: String? = null): T

Get the dependency from the map for the key represented by the type (and optionally, with the given name).

Link copied to clipboard
inline fun <T> resolveDeferred(key: String? = null): Deferred<T>

Get a deferred dependency from the map for the key represented by the type (and optionally, with the given name).

Link copied to clipboard
open override fun <T> set(key: DependencyKey, value: suspend DependencyResolver.() -> T)

Associate the given dependency key with the corresponding initializer.