2016-06-06 11:26:53 +03:00
|
|
|
Overriding of containers
|
|
|
|
------------------------
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2016-06-03 00:48:06 +03:00
|
|
|
.. currentmodule:: dependency_injector.containers
|
2015-11-20 19:52:19 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
Containers can be overridden by other containers. This, actually, means that
|
|
|
|
all of the providers from overriding container will override providers with
|
|
|
|
the same names in overridden container.
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
There are two ways to override :py:class:`DeclarativeContainer` with another
|
|
|
|
container:
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
- Use :py:meth:`DeclarativeContainer.override` method.
|
2015-11-20 19:52:19 +03:00
|
|
|
- Use :py:func:`override` class decorator.
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
Example of overriding container using :py:meth:`DeclarativeContainer.override`
|
2015-11-10 11:42:29 +03:00
|
|
|
method:
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
.. literalinclude:: ../../examples/containers/override_declarative.py
|
2015-10-11 15:34:21 +03:00
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
Example of overriding container using :py:func:`override` decorator:
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
.. literalinclude:: ../../examples/containers/override_declarative_decorator.py
|
2015-10-11 15:34:21 +03:00
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
Also there are several useful :py:class:`DeclarativeContainer` methods and
|
|
|
|
properties that help to work with container overridings:
|
2015-11-20 19:52:19 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
- :py:attr:`DeclarativeContainer.overridden` - tuple of all overriding
|
|
|
|
containers.
|
|
|
|
- :py:meth:`DeclarativeContainer.reset_last_overriding()` - reset last
|
|
|
|
overriding provider for each container providers.
|
|
|
|
- :py:meth:`DeclarativeContainer.reset_override()` - reset all overridings
|
|
|
|
for each container providers.
|
2015-11-20 19:52:19 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
:py:class:`DynamicContainer` has exactly the same functionality, except of
|
|
|
|
:py:func:`override` decorator.
|
2017-02-28 23:08:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. disqus::
|