Skip to content

Commit ac8212a

Browse files
committed
Merge branch 'release/3.42.0' into master
2 parents 31f15c0 + a305660 commit ac8212a

File tree

13 files changed

+277
-436
lines changed

13 files changed

+277
-436
lines changed

README.rst

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ Coupling and cohesion are about how tough the components are tied.
6666
- **High coupling**. If the coupling is high it's like using a superglue or welding. No easy way
6767
to disassemble.
6868
- **High cohesion**. High cohesion is like using the screws. Very easy to disassemble and
69-
assemble back or assemble a different way. It is an alternative to high coupling.
69+
assemble back or assemble a different way. It is an opposite to high coupling.
7070

7171
When the cohesion is high the coupling is low.
7272

73-
High cohesion brings the flexibility. Your code becomes easier to change and test.
73+
Low coupling brings a flexibility. Your code becomes easier to change and test.
7474

7575
How to implement dependency injection?
7676
--------------------------------------
@@ -124,7 +124,6 @@ After:
124124
if __name__ == '__main__':
125125
service = Service(ApiClient(os.getenv('API_KEY'), os.getenv('TIMEOUT')))
126126
127-
128127
``ApiClient`` is decoupled from knowing where the options come from. You can read a key and a
129128
timeout from a configuration file or even get them from a database.
130129

@@ -133,16 +132,23 @@ stub or other compatible object.
133132

134133
Flexibility comes with a price.
135134

136-
Now you need to assemble your objects like this
137-
``Service(ApiClient(os.getenv('API_KEY'), os.getenv('TIMEOUT')))``. The assembly code might get
138-
duplicated and it'll become harder to change the application structure.
135+
Now you need to assemble the objects like this::
136+
137+
service = Service(ApiClient(os.getenv('API_KEY'), os.getenv('TIMEOUT')))
138+
139+
The assembly code might get duplicated and it'll become harder to change the application structure.
140+
141+
Here comes the ``Dependency Injector``.
139142

140-
What does Dependency Injector do?
141-
---------------------------------
143+
What does the Dependency Injector do?
144+
-------------------------------------
145+
146+
With the dependency injection pattern objects lose the responsibility of assembling the
147+
dependencies. The ``Dependency Injector`` absorbs that responsibility.
142148

143149
``Dependency Injector`` helps to assemble the objects.
144150

145-
It provides you the container and the providers that help you describe objects assembly. When you
151+
It provides a container and providers that help you with the objects assembly. When you
146152
need an object you get it from the container. The rest of the assembly work is done by the
147153
framework:
148154

@@ -151,19 +157,6 @@ framework:
151157
from dependency_injector import containers, providers
152158
153159
154-
class ApiClient:
155-
156-
def __init__(self, api_key: str, timeout: int):
157-
self.api_key = api_key
158-
self.timeout = timeout
159-
160-
161-
class Service:
162-
163-
def __init__(self, api_client: ApiClient):
164-
self.api_client = api_client
165-
166-
167160
class Container(containers.DeclarativeContainer):
168161
169162
config = providers.Configuration()
@@ -187,12 +180,14 @@ framework:
187180
188181
service = container.service()
189182
190-
Retrieving of the ``Service`` instance now is done like this ``container.service()``.
183+
Retrieving of the ``Service`` instance now is done like this::
184+
185+
service = container.service()
191186

192187
Objects assembling is consolidated in the container. When you need to make a change you do it in
193188
one place.
194189

195-
When doing the testing you call the ``container.api_client.override()`` to replace the real API
190+
When doing a testing you call the ``container.api_client.override()`` to replace the real API
196191
client with a mock:
197192

198193
.. code-block:: python
@@ -203,10 +198,10 @@ client with a mock:
203198
with container.api_client.override(mock.Mock()):
204199
service = container.service()
205200
206-
It helps in a testing. Also you can use it for configuring project for the different environments:
207-
replace an API client with a stub on the dev or stage.
201+
You can override any provider by another provider.
208202

209-
`More examples <https://github.com/ets-labs/python-dependency-injector/tree/master/examples>`_
203+
It also helps you in configuring project for the different environments: replace an API client
204+
with a stub on the dev or stage.
210205

211206
Installation
212207
------------

0 commit comments

Comments
 (0)