2016-06-06 08:26:53 +00:00
|
|
|
Overriding of containers
|
|
|
|
------------------------
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2016-06-02 21:48:06 +00:00
|
|
|
.. currentmodule:: dependency_injector.containers
|
2015-11-20 16:52:19 +00:00
|
|
|
|
2016-06-06 08:26:53 +00: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 12:34:21 +00:00
|
|
|
|
2016-06-06 08:26:53 +00:00
|
|
|
There are two ways to override :py:class:`DeclarativeContainer` with another
|
|
|
|
container:
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2016-06-06 08:26:53 +00:00
|
|
|
- Use :py:meth:`DeclarativeContainer.override` method.
|
2015-11-20 16:52:19 +00:00
|
|
|
- Use :py:func:`override` class decorator.
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2016-06-06 08:26:53 +00:00
|
|
|
Example of overriding container using :py:meth:`DeclarativeContainer.override`
|
2015-11-10 08:42:29 +00:00
|
|
|
method:
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2016-06-06 08:26:53 +00:00
|
|
|
.. literalinclude:: ../../examples/containers/override_declarative.py
|
2015-10-11 12:34:21 +00:00
|
|
|
:language: python
|
|
|
|
|
2016-06-06 08:26:53 +00:00
|
|
|
Example of overriding container using :py:func:`override` decorator:
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2016-06-06 08:26:53 +00:00
|
|
|
.. literalinclude:: ../../examples/containers/override_declarative_decorator.py
|
2015-10-11 12:34:21 +00:00
|
|
|
:language: python
|
|
|
|
|
2016-06-06 08:26:53 +00:00
|
|
|
Also there are several useful :py:class:`DeclarativeContainer` methods and
|
|
|
|
properties that help to work with container overridings:
|
2015-11-20 16:52:19 +00:00
|
|
|
|
2016-06-06 08:26:53 +00: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 16:52:19 +00:00
|
|
|
|
2016-06-06 08:26:53 +00:00
|
|
|
:py:class:`DynamicContainer` has exactly the same functionality, except of
|
|
|
|
:py:func:`override` decorator.
|
2017-02-28 20:08:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
.. disqus::
|