File tree Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ Development
17
17
`@fabiocerqueira <https://github.com/fabiocerqueira >`_, `@Jitesh-Khuttan <https://github.com/Jitesh-Khuttan >`_.
18
18
- Refactor wiring module to store all patched callable data in the ``PatchedRegistry ``.
19
19
- Improve wording on the "Dependency injection and inversion of control in Python" docs page.
20
+ - Add documentation on the ``@inject `` decorator.
20
21
- Update typing in the main example and cohesion/coupling correlation definition in
21
22
"Dependency injection and inversion of control in Python".
22
23
Thanks to `@illia-v (Illia Volochii) <https://github.com/illia-v >`_ for the
Original file line number Diff line number Diff line change @@ -22,6 +22,82 @@ To use wiring you need:
22
22
:local:
23
23
:backlinks: none
24
24
25
+ Decorator @inject
26
+ -----------------
27
+
28
+ Decorator ``@inject `` injects the dependencies. Use it to decorate all functions and methods
29
+ with the injections.
30
+
31
+ .. code-block :: python
32
+
33
+ from dependency_injector.wiring import inject, Provide
34
+
35
+
36
+ @inject
37
+ def foo (bar : Bar = Provide[Container.bar]):
38
+ ...
39
+
40
+ Decorator ``@inject `` must be specified as a very first decorator of a function to ensure that
41
+ the wiring works appropriately. This will also contribute to the performance of the wiring process.
42
+
43
+ .. code-block :: python
44
+
45
+ from dependency_injector.wiring import inject, Provide
46
+
47
+
48
+ @decorator_etc
49
+ @decorator_2
50
+ @decorator_1
51
+ @inject
52
+ def foo (bar : Bar = Provide[Container.bar]):
53
+ ...
54
+
55
+ Specifying the ``@inject `` as a first decorator is also crucial for FastAPI, other frameworks
56
+ using decorators similarly, for closures, and for any types of custom decorators with the injections.
57
+
58
+ FastAPI example:
59
+
60
+ .. code-block :: python
61
+
62
+ app = FastAPI()
63
+
64
+
65
+ @app.api_route (" /" )
66
+ @inject
67
+ async def index (service : Service = Depends(Provide[Container.service])):
68
+ value = await service.process()
69
+ return {" result" : value}
70
+
71
+ Decorators example:
72
+
73
+ .. code-block :: python
74
+
75
+ def decorator1 (func ):
76
+ @functools.wraps (func)
77
+ @inject
78
+ def wrapper (value1 : int = Provide[Container.config.value1]):
79
+ result = func()
80
+ return result + value1
81
+ return wrapper
82
+
83
+
84
+ def decorator2 (func ):
85
+ @functools.wraps (func)
86
+ @inject
87
+ def wrapper (value2 : int = Provide[Container.config.value2]):
88
+ result = func()
89
+ return result + value2
90
+ return wrapper
91
+
92
+ @decorator1
93
+ @decorator2
94
+ def sample ():
95
+ ...
96
+
97
+ .. seealso ::
98
+ `Issue #404 <https://github.com/ets-labs/python-dependency-injector/issues/404#issuecomment-785216978 >`_
99
+ explains ``@inject `` decorator in a few more details.
100
+
25
101
Markers
26
102
-------
27
103
You can’t perform that action at this time.
0 commit comments