Update Factory providers delegation docs

This commit is contained in:
Roman Mogilatov 2015-09-02 22:53:39 +03:00
parent f9b4652e74
commit b8a30601df
3 changed files with 16 additions and 17 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -105,20 +105,20 @@ Example:
Factory providers delegation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``Factory`` provider could be delegated to any other provider via any kind of
injection. Saying in other words, delegation of factories - is a way to inject
factories themselves, instead of results of their calls.
``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.
As it was mentioned earlier, ``Injection`` calls ``Factory`` if ``Factory`` is
injectable value. ``Factory`` delegation is performed by wrapping delegated
``Factory`` into special provider type - ``Delegate``, that just returns
``Factory`` itself.
Actually, there are two ways of creating factory delegates:
+ ``Delegate(Factory(...))`` - obviously wrapping factory into ``Delegate``
provider.
+ ``Factory(...).delegate()`` - calling factory ``delegate()`` method, that
+ ``di.Delegate(di.Factory(...))`` - obviously wrapping factory into
``di.Delegate`` provider.
+ ``di.Factory(...).delegate()`` - calling factory ``delegate()`` method, that
returns delegate wrapper for current factory.
Example:

View File

@ -1,7 +1,6 @@
"""`Factory` providers delegation example."""
"""`di.Factory` providers delegation example."""
from dependency_injector.providers import Factory
from dependency_injector.injections import KwArg
import dependency_injector as di
class User(object):
@ -11,7 +10,7 @@ class User(object):
def __init__(self, photos_factory):
"""Initializer.
:param photos_factory: (dependency_injector.providers.Factory) -> Photo
:param photos_factory: (di.Factory) -> Photo
"""
self.photos_factory = photos_factory
self._main_photo = None
@ -30,9 +29,9 @@ class Photo(object):
"""Example class Photo."""
# User and Photo factories:
photos_factory = Factory(Photo)
users_factory = Factory(User,
KwArg('photos_factory', photos_factory.delegate()))
photos_factory = di.Factory(Photo)
users_factory = di.Factory(User,
photos_factory=di.Delegate(photos_factory))
# Creating several User objects:
user1 = users_factory()