mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-03-03 19:25:48 +03:00
Update Factory method injections docs
This commit is contained in:
parent
b8a30601df
commit
479f50fb76
Binary file not shown.
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 31 KiB |
|
@ -86,13 +86,13 @@ Example:
|
||||||
Factory providers and method injections
|
Factory providers and method injections
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Current example shows how to create ``Factory`` of particular class with
|
Current example shows how to create ``di.Factory`` of particular class with
|
||||||
method injections. Those injections are done by calling of specified method
|
method injections. Those injections are done by calling of specified method
|
||||||
with injectable value right after object's creation and attribute injections
|
with injectable value right after object's creation and attribute injections
|
||||||
are done.
|
are done.
|
||||||
|
|
||||||
Method injections are not very popular in Python due Python best practices
|
Method injections are not very popular in Python due Python best practices
|
||||||
(usage of public attributes instead of setter methods), but it may appear in
|
(usage of public attributes instead of setter methods), but they may appear in
|
||||||
some cases.
|
some cases.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
"""`Factory` providers with method injections example."""
|
"""`di.Factory` providers with method injections example."""
|
||||||
|
|
||||||
from dependency_injector.providers import Factory
|
import dependency_injector as di
|
||||||
from dependency_injector.injections import Method
|
|
||||||
|
|
||||||
|
|
||||||
class User(object):
|
class User(object):
|
||||||
|
@ -32,11 +31,11 @@ class CreditCard(object):
|
||||||
"""Example class CreditCard."""
|
"""Example class CreditCard."""
|
||||||
|
|
||||||
# User, Photo and CreditCard factories:
|
# User, Photo and CreditCard factories:
|
||||||
credit_cards_factory = Factory(CreditCard)
|
credit_cards_factory = di.Factory(CreditCard)
|
||||||
photos_factory = Factory(Photo)
|
photos_factory = di.Factory(Photo)
|
||||||
users_factory = Factory(User,
|
users_factory = di.Factory(User,
|
||||||
Method('set_main_photo', photos_factory),
|
di.Method('set_main_photo', photos_factory),
|
||||||
Method('set_credit_card', credit_cards_factory))
|
di.Method('set_credit_card', credit_cards_factory))
|
||||||
|
|
||||||
# Creating several User objects:
|
# Creating several User objects:
|
||||||
user1 = users_factory()
|
user1 = users_factory()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user