2020-08-21 04:52:12 +03:00
|
|
|
"""Example of the injecting of provided instance attributes and items."""
|
|
|
|
|
2020-09-04 00:48:45 +03:00
|
|
|
from dependency_injector import containers, providers
|
2020-08-21 04:52:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Service:
|
|
|
|
def __init__(self):
|
2021-09-30 22:32:21 +03:00
|
|
|
self.value = "foo"
|
2020-08-21 04:52:12 +03:00
|
|
|
self.values = [self.value]
|
|
|
|
|
|
|
|
def get_value(self):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.values[item]
|
|
|
|
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
def __init__(self, value1, value2, value3, value4):
|
|
|
|
self.value1 = value1
|
|
|
|
self.value2 = value2
|
|
|
|
self.value3 = value3
|
|
|
|
self.value4 = value4
|
|
|
|
|
|
|
|
|
2020-09-04 00:48:45 +03:00
|
|
|
class Container(containers.DeclarativeContainer):
|
2020-08-21 04:52:12 +03:00
|
|
|
|
2020-09-04 00:48:45 +03:00
|
|
|
service = providers.Singleton(Service)
|
|
|
|
|
|
|
|
client_factory = providers.Factory(
|
|
|
|
Client,
|
|
|
|
value1=service.provided[0],
|
|
|
|
value2=service.provided.value,
|
|
|
|
value3=service.provided.values[0],
|
|
|
|
value4=service.provided.get_value.call(),
|
|
|
|
)
|
2020-08-21 04:52:12 +03:00
|
|
|
|
2020-09-03 00:59:31 +03:00
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2020-09-04 00:48:45 +03:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
client = container.client_factory()
|
2021-09-30 22:32:21 +03:00
|
|
|
assert client.value1 == client.value2 == client.value3 == "foo"
|