@@ -52,7 +52,7 @@ What is ``Dependency Injector``?
52
52
53
53
``Dependency Injector `` is a dependency injection framework for Python.
54
54
55
- It helps you implement the dependency injection principle.
55
+ It helps you in implementing the dependency injection principle.
56
56
57
57
What is dependency injection?
58
58
-----------------------------
@@ -70,6 +70,9 @@ Before:
70
70
71
71
.. code-block :: python
72
72
73
+ import os
74
+
75
+
73
76
class ApiClient :
74
77
75
78
def __init__ (self ):
@@ -82,10 +85,18 @@ Before:
82
85
def __init__ (self ):
83
86
self .api_client = ApiClient()
84
87
88
+
89
8000
+ if __name__ == ' __main__' :
90
+ service = Service()
91
+
92
+
85
93
After:
86
94
87
95
.. code-block :: python
88
96
97
+ import os
98
+
99
+
89
100
class ApiClient :
90
101
91
102
def __init__ (self , api_key : str , timeout : int ):
@@ -98,48 +109,82 @@ After:
98
109
def __init__ (self , api_client : ApiClient):
99
110
self .api_client = api_client
100
111
101
- Who creates the objects now? Look at the next section.
112
+
113
+ if __name__ == ' __main__' :
114
+ service = Service(ApiClient(os.getenv(' API_KEY' ), os.getenv(' TIMEOUT' )))
115
+
116
+
117
+ Flexibility comes with a price: now you need to assemble your objects like this
118
+ ``Service(ApiClient(os.getenv('API_KEY'), os.getenv('TIMEOUT'))) ``. The assembly code might get
119
+ duplicated and it'll become harder to change the application structure.
102
120
103
121
What does Dependency Injector do?
104
122
---------------------------------
105
123
106
- ``Dependency Injector `` provides you with the container and the providers that help you build
107
- your application objects when you apply dependency injection principle:
124
+ ``Dependency Injector `` helps you assemble the objects.
125
+
126
+ It provides you the container and the providers that help you describe objects assembly. When you
127
+ need an object you get it from the container. The rest of the assembly work is done by the
128
+ framework:
108
129
109
130
.. code-block :: python
110
131
111
- from dependency_injector import containers, providers
112
- from unittest import mock
132
+ from dependency_injector import containers, providers
133
+
134
+
135
+ class ApiClient :
136
+
137
+ def __init__ (self , api_key : str , timeout : int ):
138
+ self .api_key = api_key
139
+ self .timeout = timeout
140
+
113
141
114
- from .example_di import ApiClient, Service
142
+ class Service :
143
+
144
+ def __init__ (self , api_client : ApiClient):
145
+ self .api_client = api_client
115
146
116
147
117
- class Container (containers .DeclarativeContainer ):
148
+ class Container (containers .DeclarativeContainer ):
118
149
119
- config = providers.Configuration()
150
+ config = providers.Configuration()
120
151
121
- api_client = providers.Singleton(
122
- ApiClient,
123
- api_key = config.api_key,
124
- timeout = config.timeout,
125
- )
152
+ api_client = providers.Singleton(
153
+ ApiClient,
154
+ api_key = config.api_key,
155
+ timeout = config.timeout,
156
+ )
126
157
127
- service = providers.Factory(
128
- Service,
129
- api_client = api_client,
130
- )
158
+ service = providers.Factory(
159
+ Service,
160
+ api_client = api_client,
161
+ )
162
+
163
+
164
+ if __name__ == ' __main__' :
165
+ container = Container()
166
+ container.config.api_key.from_env(' API_KEY' )
167
+ container.config.timeout.from_env(' TIMEOUT' )
168
+
169
+ service = container.service()
170
+ assert isinstance (service.api_client, ApiClient)
171
+
172
+ Retrieving of the ``Service `` instance now is done like this ``container.service() ``.
173
+
174
+ Also ``Dependency Injector `` provides a bonus in overriding any of the providers with the
175
+ ``.override() `` method:
176
+
177
+ .. code-block :: python
131
178
179
+ from unittest import mock
132
180
133
- if __name__ == ' __main__' :
134
- container = Container()
135
- container.config.from_yaml(' config.yml' )
136
181
137
- service = container.service()
138
- assert isinstance (service.api_client, ApiClient)
182
+ with container.api_client.override(mock.Mock()):
183
+ service = container.service()
184
+ assert isinstance (service.api_client, mock.Mock)
139
185
140
- with container.api_client.override(mock.Mock()):
141
- service = container.service()
142
- assert isinstance (service.api_client, mock.Mock)
186
+ It helps in a testing. Also you can use it for configuring project for the different environments:
187
+ replace an API client with a stub on the dev or stage.
143
188
144
189
`More examples <https://github.com/ets-labs/python-dependency-injector/tree/master/examples >`_
145
190
0 commit comments