mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Add docs and example for `Factory.add_attributes()
` method
This commit is contained in:
parent
f1a3ad0b82
commit
2cab6c687a
|
@ -7,6 +7,10 @@ that were made in every particular version.
|
||||||
From version 0.7.6 *Dependency Injector* framework strictly
|
From version 0.7.6 *Dependency Injector* framework strictly
|
||||||
follows `Semantic versioning`_
|
follows `Semantic versioning`_
|
||||||
|
|
||||||
|
Development version
|
||||||
|
-------------------
|
||||||
|
- Add docs and example for ``Factory.add_attributes()`` method.
|
||||||
|
|
||||||
4.29.0
|
4.29.0
|
||||||
------
|
------
|
||||||
- Implement context manager interface for resetting a singleton provider.
|
- Implement context manager interface for resetting a singleton provider.
|
||||||
|
|
|
@ -40,6 +40,14 @@ injected following these rules:
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-
|
:lines: 3-
|
||||||
|
|
||||||
|
``Factory`` provider can inject attributes. Use ``.add_attributes()`` method to specify
|
||||||
|
attribute injections.
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/providers/factory_attribute_injections.py
|
||||||
|
:language: python
|
||||||
|
:lines: 3-
|
||||||
|
:emphasize-lines: 18-18
|
||||||
|
|
||||||
Passing arguments to the underlying providers
|
Passing arguments to the underlying providers
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
|
|
||||||
|
|
28
examples/providers/factory_attribute_injections.py
Normal file
28
examples/providers/factory_attribute_injections.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
"""`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)
|
||||||
|
service.add_attributes(clent=client)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
service = container.service()
|
||||||
|
|
||||||
|
assert isinstance(service.client, Client)
|
Loading…
Reference in New Issue
Block a user