File tree Expand file tree Collapse file tree 2 files changed +103
-0
lines changed Expand file tree Collapse file tree 2 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -88,6 +88,82 @@ Also you can use ``Provide`` marker to inject a container.
8888 :emphasize-lines: 16-19
8989 :lines: 3-
9090
91+ Strings identifiers
92+ -------------------
93+
94+ You can use wiring with string identifiers. String identifier should match container provider's name:
95+
96+ .. literalinclude :: ../examples/wiring/example_string_id.py
97+ :language: python
98+ :emphasize-lines: 17
99+ :lines: 3-
100+
101+ With string identifiers you don't need to use a container to specify an injection.
102+
103+ To specify an injection from a nested container use point ``. `` as a separator:
104+
105+ .. code-block :: python
106+
107+ @inject
108+ def foo (service : UserService = Provide[' services.user' ]) -> None :
109+ ...
110+
111+ You can also use injection modifiers:
112+
113+ .. code-block :: python
114+
115+ from dependency_injector.wiring import (
116+ inject,
117+ Provide,
118+ as_int,
119+ as_float,
120+ as_,
121+ required,
122+ invariant,
123+ provided,
124+ )
125+
126+
127+ @inject
128+ def foo (value : int = Provide[' config.option' , as_int()]) -> None :
129+ ...
130+
131+
132+ @inject
133+ def foo (value : float = Provide[' config.option' , as_float()]) -> None :
134+ ...
135+
136+
137+ @inject
138+ def foo (value : Decimal = Provide[' config.option' , as_(Decimal)]) -> None :
139+ ...
140+
141+ @inject
142+ def foo (value : str = Provide[' config.option' , required()]) -> None :
143+ ...
144+
145+ @inject
146+ def foo (value : int = Provide[' config.option' , required().as_int()]) -> None :
147+ ...
148+
149+
150+ @inject
151+ def foo (value : int = Provide[' config.option' , invariant(' config.switch' )]) -> None :
152+ ...
153+
154+ @inject
155+ def foo (value : int = Provide[' service' , provided().foo[' bar' ].call()]) -> None :
156+ ...
157+
158+
159+ To inject a container use special identifier ``<container> ``:
160+
161+ .. code-block :: python
162+
163+ @inject
164+ def foo (container : Container = Provide[' <container>' ]) -> None :
165+ ...
166+
91167 Wiring with modules and packages
92168--------------------------------
93169
Original file line number Diff line number Diff line change 1+ """Wiring string id example."""
2+
3+ import sys
4+
5+ from dependency_injector import containers , providers
6+ from dependency_injector .wiring import inject , Provide
7+
8+
9+ class Service :
10+ ...
11+
12+
13+ class Container (containers .DeclarativeContainer ):
14+
15+ service = providers .Factory (Service )
16+
17+
18+ @inject
19+ def main (service : Service = Provide ['service' ]) -> None :
20+ ...
21+
22+
23+ if __name__ == '__main__' :
24+ container = Container ()
25+ container .wire (modules = [sys .modules [__name__ ]])
26+
27+ main ()
You can’t perform that action at this time.
0 commit comments