mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
Update Factory providers delegation docs
This commit is contained in:
parent
f9b4652e74
commit
b8a30601df
Binary file not shown.
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 20 KiB |
|
@ -105,20 +105,20 @@ Example:
|
||||||
Factory providers delegation
|
Factory providers delegation
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
``Factory`` provider could be delegated to any other provider via any kind of
|
``di.Factory`` provider could be delegated to any other provider via any kind
|
||||||
injection. Saying in other words, delegation of factories - is a way to inject
|
of injection. As it was mentioned earlier, if ``di.Factory`` is injectable
|
||||||
factories themselves, instead of results of their calls.
|
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:
|
Actually, there are two ways of creating factory delegates:
|
||||||
|
|
||||||
+ ``Delegate(Factory(...))`` - obviously wrapping factory into ``Delegate``
|
+ ``di.Delegate(di.Factory(...))`` - obviously wrapping factory into
|
||||||
provider.
|
``di.Delegate`` provider.
|
||||||
+ ``Factory(...).delegate()`` - calling factory ``delegate()`` method, that
|
+ ``di.Factory(...).delegate()`` - calling factory ``delegate()`` method, that
|
||||||
returns delegate wrapper for current factory.
|
returns delegate wrapper for current factory.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
"""`Factory` providers delegation example."""
|
"""`di.Factory` providers delegation example."""
|
||||||
|
|
||||||
from dependency_injector.providers import Factory
|
import dependency_injector as di
|
||||||
from dependency_injector.injections import KwArg
|
|
||||||
|
|
||||||
|
|
||||||
class User(object):
|
class User(object):
|
||||||
|
@ -11,7 +10,7 @@ class User(object):
|
||||||
def __init__(self, photos_factory):
|
def __init__(self, photos_factory):
|
||||||
"""Initializer.
|
"""Initializer.
|
||||||
|
|
||||||
:param photos_factory: (dependency_injector.providers.Factory) -> Photo
|
:param photos_factory: (di.Factory) -> Photo
|
||||||
"""
|
"""
|
||||||
self.photos_factory = photos_factory
|
self.photos_factory = photos_factory
|
||||||
self._main_photo = None
|
self._main_photo = None
|
||||||
|
@ -30,9 +29,9 @@ class Photo(object):
|
||||||
"""Example class Photo."""
|
"""Example class Photo."""
|
||||||
|
|
||||||
# User and Photo factories:
|
# User and Photo factories:
|
||||||
photos_factory = Factory(Photo)
|
photos_factory = di.Factory(Photo)
|
||||||
users_factory = Factory(User,
|
users_factory = di.Factory(User,
|
||||||
KwArg('photos_factory', photos_factory.delegate()))
|
photos_factory=di.Delegate(photos_factory))
|
||||||
|
|
||||||
# Creating several User objects:
|
# Creating several User objects:
|
||||||
user1 = users_factory()
|
user1 = users_factory()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user