Scope

interface Scope

Represents a lexical scope that holds and manages dependency instances. Scopes can be nested to create a hierarchy, allowing for managing dependencies with different lifecycles. Instances resolved in a child scope can access dependencies from its parent scope.

Inheritors

Functions

Link copied to clipboard
abstract fun closeAll()

Closes this scope and clears all of its cached singleton instances. This is typically called when the lifecycle associated with this scope ends (e.g., Activity.onDestroy()). Note: This does not close parent or child scopes.

Link copied to clipboard
abstract fun declare(qualifier: Qualifier, instance: Any)

Manually declares and adds a pre-existing instance to this scope's cache. This is useful for binding instances that are not created by the DI framework itself, such as Android's Context.

Link copied to clipboard
abstract fun get(qualifier: Qualifier): Any

Resolves and returns a dependency instance that matches the given qualifier. If the instance is a singleton and already created, it returns the cached instance. If it's a factory, a new instance is created on each call. If the instance is not found in the current scope, it will search in the parent scope.

Link copied to clipboard
abstract fun getSubScope(qualifier: Qualifier): Scope

Retrieves a direct child scope identified by the given qualifier.

Link copied to clipboard
abstract fun registerFactory(vararg modules: DependencyModule)

Registers one or more DependencyModules into the scope. This populates the scope with the necessary factories to create dependency instances.

Link copied to clipboard
abstract fun resolvePath(path: Path): Scope

Navigates through the scope hierarchy and returns a descendant scope specified by the path. The path is resolved from the current scope downwards.