mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-16 03:23:13 +03:00
Update factory init injections example and picture
This commit is contained in:
parent
7da1a38718
commit
a606baa637
Binary file not shown.
Before Width: | Height: | Size: 32 KiB |
|
@ -24,18 +24,11 @@ injected following these rules:
|
||||||
+ Keyword context arguments have the priority over the ``Factory`` keyword dependencies with the
|
+ Keyword context arguments have the priority over the ``Factory`` keyword dependencies with the
|
||||||
same name.
|
same name.
|
||||||
|
|
||||||
For example, if injectable value of injection is a :py:class:`Factory`, it
|
.. image:: images/factory_init_injections.png
|
||||||
will provide new one instance (as a result of its call) every time, when
|
|
||||||
injection needs to be done.
|
|
||||||
|
|
||||||
Example below is a little bit more complicated. It shows how to create
|
|
||||||
:py:class:`Factory` of particular class with ``__init__()`` injections which
|
|
||||||
injectable values are also provided by another factories:
|
|
||||||
|
|
||||||
.. image:: /images/providers/factory_init_injections.png
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/providers/factory_init_injections.py
|
.. literalinclude:: ../../examples/providers/factory_init_injections.py
|
||||||
:language: python
|
:language: python
|
||||||
|
:lines: 3-
|
||||||
|
|
||||||
Factory providers and building complex object graphs
|
Factory providers and building complex object graphs
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
BIN
docs/providers/images/factory_init_injections.png
Normal file
BIN
docs/providers/images/factory_init_injections.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
|
@ -1,39 +1,37 @@
|
||||||
"""`Factory` providers init injections example."""
|
"""`Factory` providers init injections example."""
|
||||||
|
|
||||||
import collections
|
from dependency_injector import providers
|
||||||
|
|
||||||
import dependency_injector.providers as providers
|
|
||||||
|
|
||||||
|
|
||||||
CreditCard = collections.namedtuple('CreditCard', [])
|
class Photo:
|
||||||
Photo = collections.namedtuple('Photo', [])
|
...
|
||||||
User = collections.namedtuple('User', ['uid', 'main_photo', 'credit_card'])
|
|
||||||
|
|
||||||
# User, Photo and CreditCard factories:
|
|
||||||
credit_cards_factory = providers.Factory(CreditCard)
|
|
||||||
photos_factory = providers.Factory(Photo)
|
|
||||||
users_factory = providers.Factory(User,
|
|
||||||
main_photo=photos_factory,
|
|
||||||
credit_card=credit_cards_factory)
|
|
||||||
|
|
||||||
# Creating several User objects:
|
|
||||||
user1 = users_factory(1)
|
|
||||||
# Same as: user1 = User(1,
|
|
||||||
# main_photo=Photo(),
|
|
||||||
# credit_card=CreditCard())
|
|
||||||
user2 = users_factory(2)
|
|
||||||
# Same as: user2 = User(2,
|
|
||||||
# main_photo=Photo(),
|
|
||||||
# credit_card=CreditCard())
|
|
||||||
|
|
||||||
|
|
||||||
# Context keyword arguments have priority on keyword argument injections:
|
class User:
|
||||||
main_photo = Photo()
|
|
||||||
credit_card = CreditCard()
|
|
||||||
|
|
||||||
user3 = users_factory(3,
|
def __init__(self, uid: int, main_photo: Photo):
|
||||||
main_photo=main_photo,
|
self.uid = uid
|
||||||
credit_card=credit_card)
|
self.main_photo = main_photo
|
||||||
# Same as: user3 = User(3,
|
|
||||||
# main_photo=main_photo,
|
|
||||||
# credit_card=credit_card)
|
photo_factory = providers.Factory(Photo)
|
||||||
|
user_factory = providers.Factory(
|
||||||
|
User,
|
||||||
|
main_photo=photo_factory,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
user1 = user_factory(1)
|
||||||
|
# Same as: # user1 = User(1, main_photo=Photo())
|
||||||
|
|
||||||
|
user2 = user_factory(2)
|
||||||
|
# Same as: # user2 = User(2, main_photo=Photo())
|
||||||
|
|
||||||
|
# Context keyword arguments have a priority:
|
||||||
|
another_photo = Photo()
|
||||||
|
user3 = user_factory(
|
||||||
|
uid=3,
|
||||||
|
main_photo=another_photo,
|
||||||
|
)
|
||||||
|
# Same as: # user3 = User(uid=3, main_photo=another_photo)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user