DependencyContainer
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.
Note
Fastapi users MUST use = .wire() method without arguments when injecting dependencies.
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)
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.
register_all_in_module(module, pattern='*')
Register all modules inside a given module.
Useful when your components reside in one place, and you'd like to avoid having to @register each of them.
Alternatively this can be used if you want to use the library without having to rely on decorators.
See Also: self.initialization_context to wire parameters without having to use a default value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module |
ModuleType
|
The package name to recursively search for classes. |
required |
pattern |
str
|
A pattern that will be fed to fnmatch to determine if a class will be registered or not. |
'*'
|