mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
1ad852d193
* Update index page * Update providers index page * Make a little wording fix on containers index page * Refactor factory provider docs header * Update factory injection docs * Update factory init injections example and picture * Start work on underlying providers * Finish the docs for factory arguments to the underlying providers * Edit providers delegation section * Edit section about specialized factory provider * Edit abstract factory section * Edit FactoryAggregate docs * Add meta keywords and description
38 lines
821 B
Python
38 lines
821 B
Python
"""`Factory` providers delegation example."""
|
|
|
|
from typing import Callable, List
|
|
|
|
from dependency_injector import providers
|
|
|
|
|
|
class User:
|
|
def __init__(self, uid: int) -> None:
|
|
self.uid = uid
|
|
|
|
|
|
class UserRepository:
|
|
def __init__(self, user_factory: Callable[..., User]) -> None:
|
|
self.user_factory = user_factory
|
|
|
|
def get_all(self) -> List[User]:
|
|
return [
|
|
self.user_factory(**user_data)
|
|
for user_data in [{'uid': 1}, {'uid': 2}]
|
|
]
|
|
|
|
|
|
user_factory = providers.Factory(User)
|
|
user_repository_factory = providers.Factory(
|
|
UserRepository,
|
|
user_factory=user_factory.provider,
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
user_repository = user_repository_factory()
|
|
|
|
user1, user2 = user_repository.get_all()
|
|
|
|
assert user1.uid == 1
|
|
assert user2.uid == 2
|