2020-08-16 05:09:41 +03:00
|
|
|
Declarative container
|
|
|
|
---------------------
|
2016-06-03 00:48:06 +03:00
|
|
|
|
|
|
|
.. currentmodule:: dependency_injector.containers
|
|
|
|
|
2020-08-16 05:09:41 +03:00
|
|
|
:py:class:`DeclarativeContainer` is a class-based style of the providers definition.
|
2016-06-03 00:48:06 +03:00
|
|
|
|
2020-08-16 05:09:41 +03:00
|
|
|
You create the declarative container subclass, put the providers as attributes and create the
|
|
|
|
container instance.
|
2016-06-03 00:48:06 +03:00
|
|
|
|
2020-08-14 23:27:17 +03:00
|
|
|
.. literalinclude:: ../../examples/containers/declarative.py
|
|
|
|
:language: python
|
|
|
|
:lines: 3-
|
2016-06-03 00:48:06 +03:00
|
|
|
|
2020-08-16 05:09:41 +03:00
|
|
|
The declarative container providers should only be used when you have the container instance.
|
|
|
|
Working with the providers of the container on the class level will influence all further
|
|
|
|
instances.
|
2016-06-03 00:48:06 +03:00
|
|
|
|
2020-08-16 05:09:41 +03:00
|
|
|
The declarative container can not have any methods or any other attributes then providers.
|
2016-06-03 00:48:06 +03:00
|
|
|
|
2020-08-14 23:27:17 +03:00
|
|
|
The container class provides next attributes:
|
2016-06-03 00:48:06 +03:00
|
|
|
|
2020-08-14 23:27:17 +03:00
|
|
|
- ``providers`` - the dictionary of all the container providers
|
|
|
|
- ``cls_providers`` - the dictionary of the container providers of the current container
|
|
|
|
- ``inherited_providers`` - the dictionary of all the inherited container providers
|
2016-06-03 00:48:06 +03:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/containers/declarative_inheritance.py
|
|
|
|
:language: python
|
2020-08-14 23:27:17 +03:00
|
|
|
:lines: 3-
|
2016-06-03 00:48:06 +03:00
|
|
|
|
2020-08-14 23:27:17 +03:00
|
|
|
Injections in the declarative container are done the usual way:
|
2016-06-03 00:48:06 +03:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/containers/declarative_injections.py
|
|
|
|
:language: python
|
2020-08-14 23:27:17 +03:00
|
|
|
:lines: 3-
|
2017-02-28 23:08:52 +03:00
|
|
|
|
2020-08-14 23:27:17 +03:00
|
|
|
You can override the container providers when you create the container instance:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/containers/declarative_override_providers.py
|
|
|
|
:language: python
|
|
|
|
:lines: 3-
|
2017-02-28 23:08:52 +03:00
|
|
|
|
|
|
|
.. disqus::
|