mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Release 1.16.7
This commit is contained in:
parent
4e70d138fe
commit
cbc8d70151
|
@ -61,7 +61,7 @@ from dependency_injector.errors import (
|
|||
from dependency_injector import catalogs
|
||||
catalog = catalogs
|
||||
|
||||
VERSION = '1.16.5'
|
||||
VERSION = '1.16.7'
|
||||
"""Version number that follows semantic versioning.
|
||||
|
||||
:type: str
|
||||
|
|
|
@ -93,7 +93,7 @@ Example
|
|||
|
||||
Let's go through next example:
|
||||
|
||||
.. literalinclude:: ../../examples/ioc_di_demos/car_engine_1.py
|
||||
.. literalinclude:: ../../examples/ioc_di_demos/car_engine.py
|
||||
:language: python
|
||||
:linenos:
|
||||
|
||||
|
@ -101,6 +101,27 @@ Let's go through next example:
|
|||
more sense then creating an ``Engine`` separatelly and then
|
||||
**put (inject) it into** ``Car`` when ``Car`` is being created?
|
||||
|
||||
.. literalinclude:: ../../examples/ioc_di_demos/car_engine_2.py
|
||||
.. literalinclude:: ../../examples/ioc_di_demos/car_engine_ioc.py
|
||||
:language: python
|
||||
:linenos:
|
||||
|
||||
Previous example may look more obvious and gives a chance to start getting
|
||||
other benefits of dependency injection and inversion of control, but creation
|
||||
of ``Car`` instances became a bit harder cause now ``Engine`` injections
|
||||
should be done manually every time when ``Car`` instances are being created.
|
||||
|
||||
Let's automate ``Engine`` into ``Car`` injections using *Dependency Injector*:
|
||||
|
||||
|
||||
.. literalinclude:: ../../examples/ioc_di_demos/car_engine_ioc_container.py
|
||||
:language: python
|
||||
:linenos:
|
||||
|
||||
.. note::
|
||||
|
||||
``Components`` from previous example is an inversion of control container.
|
||||
It contains a collection of component providers that could be injected
|
||||
into each other.
|
||||
|
||||
Assuming this, ``Components`` could be one and the only place, where
|
||||
application's structure is being managed on the high level.
|
||||
|
|
|
@ -88,28 +88,12 @@ Let's go through the code of ``example.py``:
|
|||
:linenos:
|
||||
|
||||
At some point, things defined above mean, that the code from ``example.py``,
|
||||
could look different, like in ``ioc_example.py``:
|
||||
could look different, like in ``example_ioc.py``:
|
||||
|
||||
.. literalinclude:: ../../examples/ioc_di_demos/ioc_example.py
|
||||
.. literalinclude:: ../../examples/ioc_di_demos/example_ioc.py
|
||||
:language: python
|
||||
:linenos:
|
||||
|
||||
Also the code from ``ioc_example.py`` could be upgraded with inversion of
|
||||
control container, like in ``ioc_container_example.py``:
|
||||
|
||||
.. literalinclude:: ../../examples/ioc_di_demos/ioc_container_example.py
|
||||
:language: python
|
||||
:linenos:
|
||||
|
||||
.. note::
|
||||
|
||||
``Components`` from ``ioc_container_example.py`` is an IoC container. It
|
||||
contains a collection of component providers that could be injected into
|
||||
each other.
|
||||
|
||||
Assuming this, ``Components`` could be one and the only place, where
|
||||
application's structure is being managed on the high level.
|
||||
|
||||
Best explanation, ever
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
|
|
@ -11,6 +11,10 @@ Development version
|
|||
-------------------
|
||||
- No features.
|
||||
|
||||
1.16.7
|
||||
------
|
||||
- Add some changes into introduction section of documentation.
|
||||
|
||||
1.16.5
|
||||
------
|
||||
- Move project to ``https://github.com/ets-labs/python-dependency-injector``.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
"""Car & Engine example 1."""
|
||||
"""Car & Engine example."""
|
||||
|
||||
|
||||
class Engine(object):
|
||||
|
@ -14,5 +14,4 @@ class Car(object):
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
car = Car()
|
||||
assert car.engine is not None
|
||||
car = Car() # Application creates Car's instance
|
|
@ -1,4 +1,4 @@
|
|||
"""Car & Engine example 2."""
|
||||
"""Refactored Car & Engine example that demostrates inversion of control."""
|
||||
|
||||
|
||||
class Engine(object):
|
||||
|
@ -14,5 +14,5 @@ class Car(object):
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
car = Car(Engine())
|
||||
assert car.engine is not None
|
||||
engine = Engine() # Application creates Engine's instance
|
||||
car = Car(engine) # and inject it into the Car's instance
|
19
examples/ioc_di_demos/car_engine_ioc_container.py
Normal file
19
examples/ioc_di_demos/car_engine_ioc_container.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
"""Example of inversion of control container for Car & Engine example."""
|
||||
|
||||
from dependency_injector import catalogs
|
||||
from dependency_injector import providers
|
||||
|
||||
from car_engine_ioc import Car
|
||||
from car_engine_ioc import Engine
|
||||
|
||||
|
||||
class Components(catalogs.DeclarativeCatalog):
|
||||
"""Catalog of component providers."""
|
||||
|
||||
engine = providers.Factory(Engine)
|
||||
|
||||
car = providers.Factory(Car, engine=engine)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
car = Components.car() # Application creates Car's instance
|
|
@ -1,19 +0,0 @@
|
|||
"""The Code, that uses IoC container."""
|
||||
|
||||
from dependency_injector import catalogs
|
||||
from dependency_injector import providers
|
||||
|
||||
from ioc_example import Service
|
||||
from ioc_example import Client
|
||||
|
||||
|
||||
class Components(catalogs.DeclarativeCatalog):
|
||||
"""Catalog of component providers."""
|
||||
|
||||
service = providers.Factory(Service)
|
||||
|
||||
client = providers.Factory(Client, service=service)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = Components.client() # Application creates Client's instance
|
Loading…
Reference in New Issue
Block a user