2020-09-01 23:04:48 +03:00
|
|
|
"""`Factory` provider init injections example."""
|
2016-06-08 16:39:53 +03:00
|
|
|
|
2020-09-03 23:46:03 +03:00
|
|
|
from dependency_injector import containers, providers
|
2020-09-01 04:26:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Photo:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class User:
|
|
|
|
def __init__(self, uid: int, main_photo: Photo) -> None:
|
|
|
|
self.uid = uid
|
|
|
|
self.main_photo = main_photo
|
|
|
|
|
|
|
|
|
2020-09-03 23:46:03 +03:00
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
photo_factory = providers.Factory(Photo)
|
|
|
|
|
|
|
|
user_factory = providers.Factory(
|
|
|
|
User,
|
|
|
|
main_photo=photo_factory,
|
|
|
|
)
|
2020-09-01 04:26:21 +03:00
|
|
|
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2020-09-03 23:46:03 +03:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
user1 = container.user_factory(1)
|
2020-09-01 04:26:21 +03:00
|
|
|
# Same as: # user1 = User(1, main_photo=Photo())
|
|
|
|
|
2020-09-03 23:46:03 +03:00
|
|
|
user2 = container.user_factory(2)
|
2020-09-01 04:26:21 +03:00
|
|
|
# Same as: # user2 = User(2, main_photo=Photo())
|
|
|
|
|
|
|
|
another_photo = Photo()
|
2020-09-03 23:46:03 +03:00
|
|
|
user3 = container.user_factory(
|
2020-09-01 04:26:21 +03:00
|
|
|
uid=3,
|
|
|
|
main_photo=another_photo,
|
|
|
|
)
|
|
|
|
# Same as: # user3 = User(uid=3, main_photo=another_photo)
|