mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Add container usage to provided instance examples
This commit is contained in:
parent
4f111aae9b
commit
d6e4e8fb08
|
@ -14,7 +14,7 @@ You can inject provided object attribute, item or result of its method call.
|
|||
|
||||
.. literalinclude:: ../../examples/providers/provided_instance.py
|
||||
:language: python
|
||||
:emphasize-lines: 26-32
|
||||
:emphasize-lines: 28-34
|
||||
:lines: 3-
|
||||
|
||||
To use the feature you should use the ``.provided`` attribute of the injected provider. This
|
||||
|
@ -32,7 +32,7 @@ You can do nested constructions:
|
|||
|
||||
.. literalinclude:: ../../examples/providers/provided_instance_complex.py
|
||||
:language: python
|
||||
:emphasize-lines: 24-30
|
||||
:emphasize-lines: 26-32
|
||||
:lines: 3-
|
||||
|
||||
The ``.provided`` attribute is available for the next providers:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Example of the injecting of provided instance attributes and items."""
|
||||
|
||||
from dependency_injector import providers
|
||||
from dependency_injector import containers, providers
|
||||
|
||||
|
||||
class Service:
|
||||
|
@ -23,17 +23,21 @@ class Client:
|
|||
self.value4 = value4
|
||||
|
||||
|
||||
service = providers.Singleton(Service)
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
client_factory = providers.Factory(
|
||||
Client,
|
||||
value1=service.provided[0],
|
||||
value2=service.provided.value,
|
||||
value3=service.provided.values[0],
|
||||
value4=service.provided.get_value.call(),
|
||||
)
|
||||
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(),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = client_factory()
|
||||
container = Container()
|
||||
|
||||
client = container.client_factory()
|
||||
assert client.value1 == client.value2 == client.value3 == 'foo'
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Complex example of the injecting of provided instance attributes and items."""
|
||||
|
||||
from dependency_injector import providers
|
||||
from dependency_injector import containers, providers
|
||||
|
||||
|
||||
class Service:
|
||||
|
@ -12,31 +12,35 @@ class Service:
|
|||
return self.value
|
||||
|
||||
|
||||
service = providers.Singleton(Service, value=42)
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
dependency = providers.Object(
|
||||
{
|
||||
'foo': {
|
||||
'bar': 10,
|
||||
'baz': lambda arg: {'arg': arg}
|
||||
service = providers.Singleton(Service, value=42)
|
||||
|
||||
dependency = providers.Object(
|
||||
{
|
||||
'foo': {
|
||||
'bar': 10,
|
||||
'baz': lambda arg: {'arg': arg}
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
demo_list = providers.List(
|
||||
dependency.provided['foo']['bar'],
|
||||
dependency.provided['foo']['baz'].call(22)['arg'],
|
||||
dependency.provided['foo']['baz'].call(service)['arg'],
|
||||
dependency.provided['foo']['baz'].call(service)['arg'].value,
|
||||
dependency.provided['foo']['baz'].call(service)['arg'].get_value.call(),
|
||||
)
|
||||
demo_list = providers.List(
|
||||
dependency.provided['foo']['bar'],
|
||||
dependency.provided['foo']['baz'].call(22)['arg'],
|
||||
dependency.provided['foo']['baz'].call(service)['arg'],
|
||||
dependency.provided['foo']['baz'].call(service)['arg'].value,
|
||||
dependency.provided['foo']['baz'].call(service)['arg'].get_value.call(),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
assert demo_list() == [
|
||||
container = Container()
|
||||
|
||||
assert container.demo_list() == [
|
||||
10,
|
||||
22,
|
||||
service(),
|
||||
container.service(),
|
||||
42,
|
||||
42,
|
||||
]
|
||||
|
|
|
@ -283,8 +283,8 @@ class Selector(Provider):
|
|||
|
||||
|
||||
class ProvidedInstanceFluentInterface:
|
||||
def __getattr__(self, item: str) -> AttributeGetter: ...
|
||||
def __getitem__(self, item: str) -> ItemGetter: ...
|
||||
def __getattr__(self, item: Any) -> AttributeGetter: ...
|
||||
def __getitem__(self, item: Any) -> ItemGetter: ...
|
||||
def call(self, *args: Injection, **kwargs: Injection) -> MethodCaller: ...
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user