2015-07-16 00:48:30 +03:00
|
|
|
Callable providers
|
|
|
|
------------------
|
|
|
|
|
2015-11-30 00:30:48 +03:00
|
|
|
.. currentmodule:: dependency_injector.providers
|
2015-11-23 16:39:19 +03:00
|
|
|
|
2016-06-09 01:43:25 +03:00
|
|
|
:py:class:`Callable` provider calls wrapped callable on every call.
|
2015-07-16 00:48:30 +03:00
|
|
|
|
2015-07-20 19:31:31 +03:00
|
|
|
Callable providers and injections
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2016-06-09 01:43:25 +03:00
|
|
|
:py:class:`Callable` provider takes a various number of positional and keyword
|
|
|
|
arguments that are used as wrapped callable injections. Every time, when
|
|
|
|
:py:class:`Callable` provider is called, positional and keyword argument
|
|
|
|
injections would be passed as an callable arguments.
|
2015-10-22 16:51:16 +03:00
|
|
|
|
2016-06-09 01:43:25 +03:00
|
|
|
Injections are done according to the next rules:
|
2015-12-28 18:25:25 +03:00
|
|
|
|
|
|
|
+ All providers (instances of :py:class:`Provider`) are called every time
|
|
|
|
when injection needs to be done.
|
|
|
|
+ Providers could be injected "as is" (delegated), if it is defined obviously.
|
2016-06-09 01:43:25 +03:00
|
|
|
Check out :ref:`callable_providers_delegation`.
|
|
|
|
+ All other injectable values are provided *"as is"*.
|
|
|
|
+ Positional context arguments will be appended after :py:class:`Callable`
|
2015-10-22 16:51:16 +03:00
|
|
|
positional injections.
|
2016-06-09 01:43:25 +03:00
|
|
|
+ Keyword context arguments have priority on :py:class:`Callable` keyword
|
2015-10-22 16:51:16 +03:00
|
|
|
injections and will be merged over them.
|
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
Example that shows usage of :py:class:`Callable` with positional argument
|
2015-10-22 16:51:16 +03:00
|
|
|
injections:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/callable_args.py
|
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|
2015-07-16 00:48:30 +03:00
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
Next one example shows usage of :py:class:`Callable` with keyword argument
|
2015-10-22 16:51:16 +03:00
|
|
|
injections:
|
2015-07-16 00:48:30 +03:00
|
|
|
|
2015-07-25 00:51:14 +03:00
|
|
|
.. image:: /images/providers/callable.png
|
2015-07-16 00:48:30 +03:00
|
|
|
:width: 100%
|
|
|
|
:align: center
|
|
|
|
|
2015-10-22 16:51:16 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/callable_kwargs.py
|
2015-08-03 15:49:55 +03:00
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|
2015-07-20 19:31:31 +03:00
|
|
|
|
2016-06-09 01:43:25 +03:00
|
|
|
.. _callable_providers_delegation:
|
2015-12-28 18:25:25 +03:00
|
|
|
|
2015-07-20 19:31:31 +03:00
|
|
|
Callable providers delegation
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2016-06-09 01:43:25 +03:00
|
|
|
:py:class:`Callable` provider could be delegated to any other provider via
|
|
|
|
any kind of injection.
|
2015-07-20 19:31:31 +03:00
|
|
|
|
2016-06-09 01:43:25 +03:00
|
|
|
Delegation of :py:class:`Callable` providers is the same as
|
|
|
|
:py:class:`Factory` providers delegation, please follow
|
|
|
|
:ref:`factory_providers_delegation` section for examples (with exception
|
2017-04-07 01:00:52 +03:00
|
|
|
of using :py:class:`DelegatedCallable` instead of
|
2016-06-09 01:43:25 +03:00
|
|
|
:py:class:`DelegatedFactory`).
|
2017-02-28 23:07:12 +03:00
|
|
|
|
2017-04-07 01:00:52 +03:00
|
|
|
Abstract callable providers
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
:py:class:`AbstractCallable` provider is a :py:class:`Callable` provider that
|
|
|
|
must be explicitly overridden before calling.
|
|
|
|
|
|
|
|
Behaviour of :py:class:`AbstractCallable` providers is the same as of
|
|
|
|
:py:class:`AbstractFactory`, please follow :ref:`abstract_factory_providers`
|
|
|
|
section for examples (with exception of using :py:class:`AbstractCallable`
|
|
|
|
provider instead of :py:class:`AbstractFactory`).
|
2017-02-28 23:07:12 +03:00
|
|
|
|
|
|
|
.. disqus::
|