mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-06 22:50:52 +03:00
Add example
This commit is contained in:
parent
f3a54040e8
commit
9ac7cb7f22
28
examples/wiring/example_container.py
Normal file
28
examples/wiring/example_container.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
"""Wiring container injection example."""
|
||||
|
||||
import sys
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import inject, Provide
|
||||
|
||||
|
||||
class Service:
|
||||
...
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
service = providers.Factory(Service)
|
||||
|
||||
|
||||
@inject
|
||||
def main(container: Container = Provide[Container]):
|
||||
service = container.service()
|
||||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
container = Container()
|
||||
container.wire(modules=[sys.modules[__name__]])
|
||||
|
||||
main()
|
Loading…
Reference in New Issue
Block a user