DependencyContainer
Bases: BaseContainer
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
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.
clear_initialized_objects()
Drop references to initialized singleton objects.
Calling this will cause the container to drop references to initialized singletons and cause it to create new instances when they are requested to be injected.
This can be useful in tests in a unittest.TestCase.setUp
method or pytest autouse=True fixture,
allowing you to have a fresh copy of the container with no previously initialized instances
to make test cases independent of each-other.
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 |
Qualifier | None
|
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. Dependency must be either a class or a factory function.
- Use as a decorator without parameters @container.register on a factory function or class to register it.
- Use as a decorator with parameters to specify qualifier and lifetime, @container.register(qualifier=...).
- Call it directly with @container.register(some_class_or_factory, qualifier=..., lifetime=...).
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.