mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-23 05:56:19 +03:00
Add docs and the example
This commit is contained in:
parent
515dbd63cf
commit
850e1c7b36
|
@ -164,6 +164,29 @@ To inject a container use special identifier ``<container>``:
|
|||
def foo(container: Container = Provide['<container>']) -> None:
|
||||
...
|
||||
|
||||
|
||||
Making injections into modules and class attributes
|
||||
---------------------------------------------------
|
||||
|
||||
You can use wiring to make injections into modules and class attributes.
|
||||
|
||||
.. literalinclude:: ../examples/wiring/example_attribute.py
|
||||
:language: python
|
||||
:lines: 3-
|
||||
:emphasize-lines: 16,21
|
||||
|
||||
You could also use string identifiers to avoid a dependency on a container:
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 1,6
|
||||
|
||||
service: Service = Provide['service']
|
||||
|
||||
|
||||
class Main:
|
||||
|
||||
service: Service = Provide['service']
|
||||
|
||||
Wiring with modules and packages
|
||||
--------------------------------
|
||||
|
||||
|
|
31
examples/wiring/example_attribute.py
Normal file
31
examples/wiring/example_attribute.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
"""Wiring attribute example."""
|
||||
|
||||
import sys
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import Provide
|
||||
|
||||
|
||||
class Service:
|
||||
...
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
service = providers.Factory(Service)
|
||||
|
||||
|
||||
service: Service = Provide[Container.service]
|
||||
|
||||
|
||||
class Main:
|
||||
|
||||
service: Service = Provide[Container.service]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
container = Container()
|
||||
container.wire(modules=[sys.modules[__name__]])
|
||||
|
||||
assert isinstance(service, Service)
|
||||
assert isinstance(Main.service, Service)
|
Loading…
Reference in New Issue
Block a user