2015-06-10 06:53:15 +00:00
|
|
|
Factory providers
|
|
|
|
-----------------
|
|
|
|
|
2015-09-02 19:18:13 +00:00
|
|
|
``di.Factory`` provider creates new instance of specified class on every call.
|
2015-06-10 06:53:15 +00:00
|
|
|
|
|
|
|
Nothing could be better than brief example:
|
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/factory.png
|
2015-07-27 22:30:05 +00:00
|
|
|
:width: 80%
|
2015-06-26 07:21:23 +00:00
|
|
|
:align: center
|
2015-06-23 13:21:37 +00:00
|
|
|
|
2015-08-03 12:42:44 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/factory.py
|
|
|
|
:language: python
|
2015-06-10 06:53:15 +00:00
|
|
|
|
|
|
|
Factory providers and injections
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Objects can take dependencies in different forms. Some objects take init
|
|
|
|
arguments, other are using attributes setting or method calls to be
|
|
|
|
initialized. It affects how such objects need to be created and initialized,
|
2015-08-31 13:31:38 +00:00
|
|
|
and that is the place where ``dependency_injector.injections`` need to be used.
|
2015-06-10 06:53:15 +00:00
|
|
|
|
|
|
|
``Factory`` provider takes various number of positional arguments, that define
|
|
|
|
what kind of dependency injections need to be done.
|
|
|
|
|
2015-08-31 13:31:38 +00:00
|
|
|
All of those instructions are defined in ``dependency_injector.injections``
|
|
|
|
module and are subclasses of ``dependency_injector.injections.Injection``.
|
|
|
|
There are several types of injections that are used by ``Factory`` provider:
|
2015-06-10 06:53:15 +00:00
|
|
|
|
2015-07-25 01:57:20 +00:00
|
|
|
+ ``KwArg`` - injection is done by passing injectable value in object's
|
|
|
|
``__init__()`` method in time of object's creation via keyword argument.
|
|
|
|
Takes keyword name of ``__init__()`` argument and injectable value.
|
|
|
|
+ ``Attribute`` - injection is done by setting specified attribute with
|
|
|
|
injectable value right after object's creation. Takes attribute's name
|
|
|
|
and injectable value.
|
|
|
|
+ ``Method`` - injection is done by calling of specified method with
|
|
|
|
injectable value right after object's creation and attribute injections
|
|
|
|
are done. Takes method name and injectable value.
|
2015-06-10 06:53:15 +00:00
|
|
|
|
|
|
|
All ``Injection``'s injectable values are provided *"as is"*, except of
|
|
|
|
providers. Providers will be called every time, when injection needs to be
|
|
|
|
done.
|
|
|
|
|
|
|
|
|
|
|
|
Factory providers and __init__ injections
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Example below shows how to create ``Factory`` of particular class with
|
|
|
|
``__init__`` keyword argument injections which injectable values are also
|
|
|
|
provided by another factories:
|
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/factory_init_injections.png
|
2015-07-27 22:38:14 +00:00
|
|
|
:width: 90%
|
2015-07-25 01:57:20 +00:00
|
|
|
:align: center
|
2015-06-10 06:53:15 +00:00
|
|
|
|
2015-08-03 12:42:44 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/factory_init_injections.py
|
|
|
|
:language: python
|
2015-06-10 06:53:15 +00:00
|
|
|
|
|
|
|
Next example shows how ``Factory`` provider deals with positional and keyword
|
|
|
|
``__init__`` context arguments. In few words, ``Factory`` provider fully
|
|
|
|
passes positional context arguments to class's ``__init__`` method, but
|
|
|
|
keyword context arguments have priority on ``KwArg`` injections (this could be
|
2015-06-18 13:35:00 +00:00
|
|
|
useful for testing).
|
|
|
|
|
|
|
|
So, please, follow the example below:
|
2015-06-10 06:53:15 +00:00
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/factory_init_injections_and_contexts.png
|
2015-06-10 06:53:15 +00:00
|
|
|
|
2015-08-03 12:42:44 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/factory_init_injections_and_contexts.py
|
|
|
|
:language: python
|
2015-06-10 06:53:15 +00:00
|
|
|
|
|
|
|
Factory providers and attribute injections
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-02 20:02:58 +00:00
|
|
|
Example below shows how to create ``di.Factory`` of particular class with
|
2015-06-10 06:53:15 +00:00
|
|
|
attribute injections. Those injections are done by setting specified attributes
|
|
|
|
with injectable values right after object's creation.
|
|
|
|
|
2015-06-18 13:35:00 +00:00
|
|
|
Example:
|
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/factory_attribute_injections.png
|
2015-06-10 06:53:15 +00:00
|
|
|
|
2015-08-03 12:42:44 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/factory_attribute_injections.py
|
|
|
|
:language: python
|
2015-06-10 06:53:15 +00:00
|
|
|
|
|
|
|
Factory providers and method injections
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-02 19:59:06 +00:00
|
|
|
Current example shows how to create ``di.Factory`` of particular class with
|
2015-06-10 06:53:15 +00:00
|
|
|
method injections. Those injections are done by calling of specified method
|
|
|
|
with injectable value right after object's creation and attribute injections
|
|
|
|
are done.
|
|
|
|
|
|
|
|
Method injections are not very popular in Python due Python best practices
|
2015-09-02 19:59:06 +00:00
|
|
|
(usage of public attributes instead of setter methods), but they may appear in
|
2015-06-10 06:53:15 +00:00
|
|
|
some cases.
|
|
|
|
|
2015-06-18 13:35:00 +00:00
|
|
|
Example:
|
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/factory_method_injections.png
|
2015-06-10 06:53:15 +00:00
|
|
|
|
2015-08-03 12:42:44 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/factory_method_injections.py
|
|
|
|
:language: python
|
2015-06-10 06:53:15 +00:00
|
|
|
|
2015-07-20 15:46:45 +00:00
|
|
|
Factory providers delegation
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-02 19:53:39 +00:00
|
|
|
``di.Factory`` provider could be delegated to any other provider via any kind
|
|
|
|
of injection. As it was mentioned earlier, if ``di.Factory`` is injectable
|
|
|
|
value, it will be called every time when injection is done. ``di.Factory``
|
|
|
|
delegation is performed by wrapping delegated ``di.Factory`` into special
|
|
|
|
provider type - ``di.Delegate``, that just returns wrapped ``di.Factory``.
|
|
|
|
Saying in other words, delegation of factories - is a way to inject factories
|
|
|
|
themselves, instead of results of their calls.
|
2015-07-20 15:46:45 +00:00
|
|
|
|
|
|
|
|
2015-07-25 01:57:20 +00:00
|
|
|
Actually, there are two ways of creating factory delegates:
|
|
|
|
|
2015-09-02 19:53:39 +00:00
|
|
|
+ ``di.Delegate(di.Factory(...))`` - obviously wrapping factory into
|
|
|
|
``di.Delegate`` provider.
|
|
|
|
+ ``di.Factory(...).delegate()`` - calling factory ``delegate()`` method, that
|
2015-07-25 01:57:20 +00:00
|
|
|
returns delegate wrapper for current factory.
|
2015-07-20 15:46:45 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/factory_delegation.png
|
2015-07-25 01:57:20 +00:00
|
|
|
:width: 85%
|
|
|
|
:align: center
|
2015-07-20 15:46:45 +00:00
|
|
|
|
2015-08-03 12:42:44 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/factory_delegation.py
|
|
|
|
:language: python
|