mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-21 14:05:11 +03:00
Update wiring docs
This commit is contained in:
parent
e9f7131a36
commit
bbd8b78a05
|
@ -95,17 +95,17 @@ Also you can use ``Provide`` marker to inject a container.
|
||||||
|
|
||||||
.. literalinclude:: ../examples/wiring/example_container.py
|
.. literalinclude:: ../examples/wiring/example_container.py
|
||||||
:language: python
|
:language: python
|
||||||
:emphasize-lines: 16-19
|
:emphasize-lines: 14-17
|
||||||
:lines: 3-
|
:lines: 3-
|
||||||
|
|
||||||
Strings identifiers
|
String identifiers
|
||||||
-------------------
|
------------------
|
||||||
|
|
||||||
You can use wiring with string identifiers. String identifier should match provider name in the container:
|
You can use wiring with string identifiers. String identifier should match provider name in the container:
|
||||||
|
|
||||||
.. literalinclude:: ../examples/wiring/example_string_id.py
|
.. literalinclude:: ../examples/wiring/example_string_id.py
|
||||||
:language: python
|
:language: python
|
||||||
:emphasize-lines: 17
|
:emphasize-lines: 15
|
||||||
:lines: 3-
|
:lines: 3-
|
||||||
|
|
||||||
With string identifiers you don't need to use a container to specify an injection.
|
With string identifiers you don't need to use a container to specify an injection.
|
||||||
|
@ -183,19 +183,19 @@ You can use wiring to make injections into modules and class attributes.
|
||||||
.. literalinclude:: ../examples/wiring/example_attribute.py
|
.. literalinclude:: ../examples/wiring/example_attribute.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-
|
:lines: 3-
|
||||||
:emphasize-lines: 16,21
|
:emphasize-lines: 14,19
|
||||||
|
|
||||||
You could also use string identifiers to avoid a dependency on a container:
|
You could also use string identifiers to avoid a dependency on a container:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:emphasize-lines: 1,6
|
:emphasize-lines: 1,6
|
||||||
|
|
||||||
service: Service = Provide['service']
|
service: Service = Provide["service"]
|
||||||
|
|
||||||
|
|
||||||
class Main:
|
class Main:
|
||||||
|
|
||||||
service: Service = Provide['service']
|
service: Service = Provide["service"]
|
||||||
|
|
||||||
Wiring with modules and packages
|
Wiring with modules and packages
|
||||||
--------------------------------
|
--------------------------------
|
||||||
|
@ -233,7 +233,7 @@ When wiring is done functions and methods with the markers are patched to provid
|
||||||
|
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[sys.modules[__name__]])
|
container.wire(modules=[__name__])
|
||||||
|
|
||||||
foo() # <--- Argument "bar" is injected
|
foo() # <--- Argument "bar" is injected
|
||||||
|
|
||||||
|
@ -267,7 +267,7 @@ You can use that in testing to re-create and re-wire a container before each tes
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.container = Container()
|
self.container = Container()
|
||||||
self.container.wire(modules=[module1, module2])
|
self.container.wire(modules=["yourapp.module1", "yourapp.module2"])
|
||||||
self.addCleanup(self.container.unwire)
|
self.addCleanup(self.container.unwire)
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
@ -278,7 +278,7 @@ You can use that in testing to re-create and re-wire a container before each tes
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def container():
|
def container():
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[module1, module2])
|
container.wire(modules=["yourapp.module1", "yourapp.module2"])
|
||||||
yield container
|
yield container
|
||||||
container.unwire()
|
container.unwire()
|
||||||
|
|
||||||
|
@ -402,11 +402,11 @@ This is useful when you import modules dynamically.
|
||||||
from .containers import Container
|
from .containers import Container
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
container = Container()
|
container = Container()
|
||||||
register_loader_containers(container) # <--- installs import hook
|
register_loader_containers(container) # <--- installs import hook
|
||||||
|
|
||||||
module = importlib.import_module('package.module')
|
module = importlib.import_module("package.module")
|
||||||
module.foo()
|
module.foo()
|
||||||
|
|
||||||
You can register multiple containers in the import hook. For doing this call register function
|
You can register multiple containers in the import hook. For doing this call register function
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""Wiring example."""
|
"""Wiring example."""
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
from dependency_injector.wiring import inject, Provide
|
from dependency_injector.wiring import Provide, inject
|
||||||
|
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
|
@ -18,7 +18,7 @@ def main(service: Service = Provide[Container.service]) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[__name__])
|
container.wire(modules=[__name__])
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
"""Wiring attribute example."""
|
"""Wiring attribute example."""
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
from dependency_injector.wiring import Provide
|
from dependency_injector.wiring import Provide
|
||||||
|
|
||||||
|
@ -23,9 +21,9 @@ class Main:
|
||||||
service: Service = Provide[Container.service]
|
service: Service = Provide[Container.service]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[sys.modules[__name__]])
|
container.wire(modules=[__name__])
|
||||||
|
|
||||||
assert isinstance(service, Service)
|
assert isinstance(service, Service)
|
||||||
assert isinstance(Main.service, Service)
|
assert isinstance(Main.service, Service)
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
"""Wiring container injection example."""
|
"""Wiring container injection example."""
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
from dependency_injector.wiring import inject, Provide
|
from dependency_injector.wiring import Provide, inject
|
||||||
|
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
|
@ -21,8 +19,8 @@ def main(container: Container = Provide[Container]):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[sys.modules[__name__]])
|
container.wire(modules=[__name__])
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
"""Wiring string id example."""
|
"""Wiring string id example."""
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
from dependency_injector.wiring import inject, Provide
|
from dependency_injector.wiring import Provide, inject
|
||||||
|
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
|
@ -16,12 +14,12 @@ class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def main(service: Service = Provide['service']) -> None:
|
def main(service: Service = Provide["service"]) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[sys.modules[__name__]])
|
container.wire(modules=[__name__])
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
"""Flask wiring example."""
|
"""Flask wiring example."""
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
from dependency_injector.wiring import inject, Provide
|
from dependency_injector.wiring import Provide, inject
|
||||||
from flask import Flask, json
|
from flask import Flask, json
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,13 +16,13 @@ class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def index_view(service: Service = Provide[Container.service]) -> str:
|
def index_view(service: Service = Provide[Container.service]) -> str:
|
||||||
return json.dumps({'service_id': id(service)})
|
return json.dumps({"service_id": id(service)})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[sys.modules[__name__]])
|
container.wire(modules=[__name__])
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.add_url_rule('/', 'index', index_view)
|
app.add_url_rule("/", "index", index_view)
|
||||||
app.run()
|
app.run()
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
"""`Resource` - Flask request scope example."""
|
"""`Resource` - Flask request scope example."""
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
from dependency_injector.wiring import inject, Provide, Closing
|
from dependency_injector.wiring import Closing, Provide, inject
|
||||||
from flask import Flask, current_app
|
from flask import Flask, current_app
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,9 +10,9 @@ class Service:
|
||||||
|
|
||||||
|
|
||||||
def init_service() -> Service:
|
def init_service() -> Service:
|
||||||
print('Init service')
|
print("Init service")
|
||||||
yield Service()
|
yield Service()
|
||||||
print('Shutdown service')
|
print("Shutdown service")
|
||||||
|
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -25,16 +23,16 @@ class Container(containers.DeclarativeContainer):
|
||||||
@inject
|
@inject
|
||||||
def index_view(service: Service = Closing[Provide[Container.service]]):
|
def index_view(service: Service = Closing[Provide[Container.service]]):
|
||||||
assert service is current_app.container.service()
|
assert service is current_app.container.service()
|
||||||
return 'Hello World!'
|
return "Hello World!"
|
||||||
|
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[sys.modules[__name__]])
|
container.wire(modules=[__name__])
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.container = container
|
app.container = container
|
||||||
app.add_url_rule('/', 'index', view_func=index_view)
|
app.add_url_rule("/", "index", view_func=index_view)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run()
|
app.run()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user