DependencyContainer
Bases: Generic[__T]
Dependency Injection and Service Locator container registry.
This contains all the necessary information to initialize registered classes. Objects instantiated by the container are lazily loaded and initialized only on first use.
Provides the following decorators: register
, abstract
and autowire
. Use register on factory functions
and concrete classes which are to be injected from the container.
Abstract classes are to be used as interfaces and will not be injected directly, rather concrete classes
which implement them will be injected instead.
Use the autowire
decorator on methods where dependency injection must be performed.
Services will be injected automatically where possible. Parameters will have to be annotated as they cannot
be located from type alone.
Attributes
context: InitializationContext[__T]
property
The initialization context for registered targets. A map between an injection target and its dependencies.
params: ParameterBag
property
Parameter bag associated with this container.
Functions
__init__(parameter_bag)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameter_bag |
ParameterBag
|
ParameterBag instance holding parameter information. |
required |
abstract(klass)
Register a type as an interface.
This type cannot be initialized directly and one of the components implementing this will be injected instead.
autowire(fn)
Automatically inject resources from the container to the decorated methods.
Any arguments which the container does not know about will be ignored so that another decorator or framework can supply their values. This decorator can be used on both async and blocking methods.
- Classes will be automatically injected.
- Parameters need to be annotated in order for container to be able to resolve them
- When injecting an interface for which there are multiple implementations you need to supply a qualifier using annotations.
get(klass, qualifier=None)
Get an instance of the requested type.
Use this to locate services by their type but strongly prefer using injection instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qualifier |
ContainerProxyQualifierValue
|
Qualifier for the class if it was registered with one. |
None
|
klass |
type[__T]
|
Class of the dependency already registered in the container. |
required |
Returns:
Type | Description |
---|---|
__T
|
An instance of the requested object. Always returns an existing instance when one is available. |
register(obj=None, *, qualifier=None, lifetime=ServiceLifetime.SINGLETON)
Register a dependency in the container.
Use @register
without parameters on a class or with a single parameter @register(qualifier=name)
to register this with a given name when there are multiple implementations of the interface this implements.
Use @register
on a function to register that function as a factory method which produces an object
that matches its return type.
The container stores all necessary metadata for this class and the underlying class remains unmodified.
warmup()
Initialize all singleton dependencies registered in the container.
This should be executed once all services are registered with the container. Targets of autowire will not be affected.