2021-03-04 00:06:53 +03:00
|
|
|
"""`Factory` provider attribute injections example."""
|
|
|
|
|
|
|
|
from dependency_injector import containers, providers
|
|
|
|
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class Service:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.client = None
|
|
|
|
|
|
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
client = providers.Factory(Client)
|
|
|
|
|
|
|
|
service = providers.Factory(Service)
|
2021-09-13 03:10:25 +03:00
|
|
|
service.add_attributes(client=client)
|
2021-03-04 00:06:53 +03:00
|
|
|
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2021-03-04 00:06:53 +03:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
service = container.service()
|
|
|
|
|
|
|
|
assert isinstance(service.client, Client)
|