2015-07-21 09:57:13 +03:00
|
|
|
Overriding of providers
|
|
|
|
-----------------------
|
|
|
|
|
2015-11-30 00:30:48 +03:00
|
|
|
.. currentmodule:: dependency_injector.providers
|
2015-11-23 18:02:48 +03:00
|
|
|
|
2015-07-21 09:57:13 +03:00
|
|
|
Every provider could be overridden by another provider.
|
2015-07-25 04:57:20 +03:00
|
|
|
|
2016-06-09 19:40:22 +03:00
|
|
|
This gives opportunity to make system behaviour more flexible at some point.
|
2015-07-25 04:57:20 +03:00
|
|
|
The main feature is that while your code is using providers, it depends on
|
|
|
|
providers, but not on the objects that providers provide. As a result of this,
|
|
|
|
you can change providing by provider object to a different one, but still
|
2020-06-14 05:24:32 +03:00
|
|
|
compatible one, without changing your previously written code.
|
2015-07-25 04:57:20 +03:00
|
|
|
|
|
|
|
Provider overriding functionality has such interface:
|
|
|
|
|
2015-09-02 18:14:13 +03:00
|
|
|
.. image:: /images/providers/provider_override.png
|
2016-06-09 19:40:22 +03:00
|
|
|
:width: 55%
|
2015-07-25 04:57:20 +03:00
|
|
|
:align: center
|
|
|
|
|
2015-11-23 18:02:48 +03:00
|
|
|
+ :py:meth:`Provider.override()` - takes another provider that will be used
|
2015-09-02 18:14:13 +03:00
|
|
|
instead of current provider. This method could be called several times.
|
|
|
|
In such case, last passed provider would be used as overriding one.
|
2015-11-23 18:02:48 +03:00
|
|
|
+ :py:meth:`Provider.reset_override()` - resets all overriding providers.
|
|
|
|
Provider starts to behave itself like usual.
|
2016-06-09 19:40:22 +03:00
|
|
|
+ :py:meth:`Provider.reset_last_overriding()` - remove last overriding
|
|
|
|
provider from stack of overriding providers.
|
2015-07-25 04:57:20 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. image:: /images/providers/overriding_simple.png
|
|
|
|
:width: 80%
|
|
|
|
:align: center
|
|
|
|
|
2015-08-03 15:52:23 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/overriding_simple.py
|
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|
2015-07-25 04:57:20 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. image:: /images/providers/overriding_users_model.png
|
|
|
|
:width: 100%
|
|
|
|
:align: center
|
|
|
|
|
2015-08-03 15:52:23 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/overriding_users_model.py
|
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|
2017-02-28 23:07:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. disqus::
|