2020-08-16 02:09:41 +00:00
|
|
|
Declarative container
|
|
|
|
---------------------
|
2016-06-02 21:48:06 +00:00
|
|
|
|
|
|
|
.. currentmodule:: dependency_injector.containers
|
|
|
|
|
2020-08-16 02:09:41 +00:00
|
|
|
:py:class:`DeclarativeContainer` is a class-based style of the providers definition.
|
2016-06-02 21:48:06 +00:00
|
|
|
|
2020-08-16 02:09:41 +00:00
|
|
|
You create the declarative container subclass, put the providers as attributes and create the
|
|
|
|
container instance.
|
2016-06-02 21:48:06 +00:00
|
|
|
|
2020-08-14 20:27:17 +00:00
|
|
|
.. literalinclude:: ../../examples/containers/declarative.py
|
|
|
|
:language: python
|
|
|
|
:lines: 3-
|
2016-06-02 21:48:06 +00:00
|
|
|
|
2020-08-16 02:09:41 +00: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-02 21:48:06 +00:00
|
|
|
|
2020-08-16 02:09:41 +00:00
|
|
|
The declarative container can not have any methods or any other attributes then providers.
|
2016-06-02 21:48:06 +00:00
|
|
|
|
2020-08-14 20:27:17 +00:00
|
|
|
The container class provides next attributes:
|
2016-06-02 21:48:06 +00:00
|
|
|
|
2020-08-14 20:27:17 +00: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-02 21:48:06 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/containers/declarative_inheritance.py
|
|
|
|
:language: python
|
2020-08-14 20:27:17 +00:00
|
|
|
:lines: 3-
|
2016-06-02 21:48:06 +00:00
|
|
|
|
2020-08-14 20:27:17 +00:00
|
|
|
Injections in the declarative container are done the usual way:
|
2016-06-02 21:48:06 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/containers/declarative_injections.py
|
|
|
|
:language: python
|
2020-08-14 20:27:17 +00:00
|
|
|
:lines: 3-
|
2017-02-28 20:08:52 +00:00
|
|
|
|
2021-10-07 01:36:41 +00:00
|
|
|
You can override container providers while creating a container instance:
|
2020-08-14 20:27:17 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/containers/declarative_override_providers.py
|
|
|
|
:language: python
|
|
|
|
:lines: 3-
|
2021-10-07 01:36:41 +00:00
|
|
|
:emphasize-lines: 13
|
|
|
|
|
|
|
|
Alternatively, you can call ``container.override_providers()`` method when the container instance
|
|
|
|
already exists:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
:emphasize-lines: 3
|
|
|
|
|
|
|
|
container = Container()
|
|
|
|
|
|
|
|
container.override_providers(foo=mock.Mock(Foo), bar=mock.Mock(Bar))
|
|
|
|
|
|
|
|
assert isinstance(container.foo(), mock.Mock)
|
|
|
|
assert isinstance(container.bar(), mock.Mock)
|
|
|
|
|
|
|
|
You can also use ``container.override_providers()`` with a context manager to reset
|
|
|
|
provided overriding after the context is closed:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
:emphasize-lines: 3
|
|
|
|
|
|
|
|
container = Container()
|
|
|
|
|
|
|
|
with container.override_providers(foo=mock.Mock(Foo), bar=mock.Mock(Bar)):
|
|
|
|
assert isinstance(container.foo(), mock.Mock)
|
|
|
|
assert isinstance(container.bar(), mock.Mock)
|
|
|
|
|
|
|
|
assert isinstance(container.foo(), Foo)
|
|
|
|
assert isinstance(container.bar(), Bar)
|
2017-02-28 20:08:52 +00:00
|
|
|
|
|
|
|
.. disqus::
|