python-dependency-injector/docs/containers/overriding.rst

42 lines
1.4 KiB
ReStructuredText
Raw Normal View History

Overriding of containers
------------------------
2015-10-11 15:34:21 +03:00
.. currentmodule:: dependency_injector.containers
2015-11-20 19:52:19 +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
There are two ways to override :py:class:`DeclarativeContainer` with another
container:
2015-10-11 15:34:21 +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
Example of overriding container using :py:meth:`DeclarativeContainer.override`
method:
2015-10-11 15:34:21 +03:00
.. literalinclude:: ../../examples/containers/override_declarative.py
2015-10-11 15:34:21 +03:00
:language: python
Example of overriding container using :py:func:`override` decorator:
2015-10-11 15:34:21 +03:00
.. literalinclude:: ../../examples/containers/override_declarative_decorator.py
2015-10-11 15:34:21 +03:00
:language: python
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
- :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
:py:class:`DynamicContainer` has exactly the same functionality, except of
:py:func:`override` decorator.
.. disqus::