DI Module¶
The dependency injection module provides a framework for managing dependencies and lifecycle of objects in Python applications.
AbstractInstanceProvider
¶
Bases: ABC
, Generic[T]
An AbstractInstanceProvider is responsible to create instances.
create(environment, *args)
abstractmethod
¶
Create a new instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
environment
|
Environment
|
the Environment |
required |
*args
|
the required arguments |
()
|
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
the instance |
get_dependencies()
¶
return the types that i depend on ( for constructor or setter injection ). The second tuple element is the number of parameters that a construction injection will require
Returns:
Type | Description |
---|---|
(list[Type], int)
|
the type array and the number of parameters |
get_host()
¶
return the class which is responsible for creation ( e.g. the injectable class )
Returns:
Type | Description |
---|---|
Type[T]
|
Type[T]: the class which is responsible for creation |
get_module()
abstractmethod
¶
return the module name of the provider
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
the module name of the provider |
get_scope()
abstractmethod
¶
return the scope name
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
the scope name |
get_type()
abstractmethod
¶
return the type of the created instance
Returns:
Type | Description |
---|---|
Type[T]
|
Type[T: the type] |
is_eager()
abstractmethod
¶
return True, if the provider will eagerly construct instances
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
eager flag |
AmbiguousProvider(type, *providers)
¶
Bases: AbstractInstanceProvider
An AmbiguousProvider covers all cases, where fetching a class would lead to an ambiguity exception.
ClassInstanceProvider(t, eager, scope='singleton')
¶
Bases: InstanceProvider
A ClassInstanceProvider is able to create instances of type T by calling the class constructor.
DIException(message)
¶
Bases: Exception
Exception raised for errors in the injector.
DIRegistrationException(message)
¶
DIRuntimeException(message)
¶
Environment(env, features=[], parent=None)
¶
Central class that manages the lifecycle of instances and their dependencies.
Usage:
@injectable()
class Foo:
def __init__(self):
@module()
class Module:
def __init__(self):
pass
environment = Environment(Module)
foo = environment.get(Foo) # will create an instance of Foo
Creates a new Environment instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env
|
Type
|
The environment class that controls the scanning of managed objects. |
required |
parent
|
Optional[Environment]
|
Optional parent environment, whose objects are inherited. |
None
|
Factory
¶
Bases: ABC
, Generic[T]
Abstract base class for factories that create instances of type T.
FactoryInstanceProvider(factory, eager, scope)
¶
Bases: InstanceProvider
A FactoryInstanceProvider is able to create instances of type T by calling registered Factory instances.
FunctionInstanceProvider(clazz, method, eager=True, scope='singleton')
¶
Bases: InstanceProvider
A FunctionInstanceProvider is able to create instances of type T by calling specific methods annotated with 'create".
InstanceProvider(host, t, eager, scope)
¶
Lifecycle
¶
Bases: Enum
This enum defines the lifecycle phases that can be processed by lifecycle processors. Phases are:
- ON_INJECT
- ON_INIT
- ON_RUNNING
- ON_DESTROY
LifecycleProcessor()
¶
Bases: ABC
A LifecycleProcessor is used to perform any side effects on managed objects during different lifecycle phases.
PostProcessor()
¶
Bases: LifecycleProcessor
Base class for custom post processors that are executed after object creation.
Providers
¶
The Providers class is a static class used in the context of the registration and resolution of InstanceProviders.
create(eager=True, scope='singleton')
¶
Any method annotated with @create will be registered as a factory method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eager
|
bool
|
If True, the corresponding object will be created eagerly when the environment is created. |
True
|
scope
|
str
|
The scope of the factory, e.g. "singleton", "request", "environment". |
'singleton'
|
factory(eager=True, scope='singleton')
¶
Decorator that needs to be used on a class that implements the Factory interface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eager
|
bool
|
If True, the corresponding object will be created eagerly when the environment is created. |
True
|
scope
|
str
|
The scope of the factory, e.g. "singleton", "request", "environment". |
'singleton'
|
inject()
¶
Methods annotated with @inject will be called with the required dependencies injected.
inject_environment()
¶
Methods annotated with @inject_environment will be called with the Environment instance injected.
injectable(eager=True, scope='singleton')
¶
Instances of classes that are annotated with @injectable can be created by an Environment.
module(imports=None)
¶
This annotation is used to mark classes that control the discovery process of injectables based on their location
relative to the module of the class. All @injectable
s and @factory
s that are located in the same or any sub-module will
be registered and managed accordingly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
imports
|
Optional[list[Type]]
|
Optional list of imported module types |
None
|
on_destroy()
¶
Methods annotated with @on_destroy
will be called when the instance is destroyed.
on_init()
¶
Methods annotated with @on_init
will be called when the instance is created.
on_running()
¶
Methods annotated with @on_running
will be called when the container up and running.