mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-16 19:40:59 +03:00
Update What Is DI docs and example
This commit is contained in:
parent
c8115ed44e
commit
2cd80b1b4c
|
@ -74,25 +74,26 @@ Example
|
||||||
|
|
||||||
Let's go through the code of ``example.py``:
|
Let's go through the code of ``example.py``:
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/di_demo/example.py
|
.. literalinclude:: ../../../examples/ioc_di_demos/example.py
|
||||||
:language: python
|
:language: python
|
||||||
|
|
||||||
At some point, things defined above mean, that the code from ``example.py``,
|
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 ``ioc_example.py``:
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/di_demo/ioc_example.py
|
.. literalinclude:: ../../../examples/ioc_di_demos/ioc_example.py
|
||||||
:language: python
|
:language: python
|
||||||
|
|
||||||
Also the code from ``ioc_example.py`` could be powered by dependency
|
Also the code from ``ioc_example.py`` could be upgraded with inversion of
|
||||||
injection framework, like in ``di_example.py``:
|
control container, like in ``ioc_container_example.py``:
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/di_demo/di_example.py
|
.. literalinclude:: ../../../examples/ioc_di_demos/ioc_container_example.py
|
||||||
:language: python
|
:language: python
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
``Components`` from ``di_example.py`` is an IoC container. It contains a
|
``Components`` from ``ioc_container_example.py`` is an IoC container. It
|
||||||
collection of component providers that could be injected into each other.
|
contains a collection of component providers that could be injected into
|
||||||
|
each other.
|
||||||
|
|
||||||
Assuming this, ``Components`` could be one and the only place, where
|
Assuming this, ``Components`` could be one and the only place, where
|
||||||
application's structure is being managed on the high level.
|
application's structure is being managed on the high level.
|
||||||
|
|
18
examples/ioc_di_demos/car_engine_1.py
Normal file
18
examples/ioc_di_demos/car_engine_1.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
"""Car & Engine example 1."""
|
||||||
|
|
||||||
|
|
||||||
|
class Engine(object):
|
||||||
|
"""Example engine."""
|
||||||
|
|
||||||
|
|
||||||
|
class Car(object):
|
||||||
|
"""Example car."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""Initializer."""
|
||||||
|
self.engine = Engine()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
car = Car()
|
||||||
|
assert car.engine is not None
|
18
examples/ioc_di_demos/car_engine_2.py
Normal file
18
examples/ioc_di_demos/car_engine_2.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
"""Car & Engine example 2."""
|
||||||
|
|
||||||
|
|
||||||
|
class Engine(object):
|
||||||
|
"""Example engine."""
|
||||||
|
|
||||||
|
|
||||||
|
class Car(object):
|
||||||
|
"""Example car."""
|
||||||
|
|
||||||
|
def __init__(self, engine):
|
||||||
|
"""Initializer."""
|
||||||
|
self.engine = engine
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
car = Car(Engine())
|
||||||
|
assert car.engine is not None
|
|
@ -1,7 +1,10 @@
|
||||||
"""The Code, powered by Dependency Injector."""
|
"""The Code, that uses IoC container."""
|
||||||
|
|
||||||
from dependency_injector import catalogs, providers
|
from dependency_injector import catalogs
|
||||||
from ioc_example import Service, Client
|
from dependency_injector import providers
|
||||||
|
|
||||||
|
from ioc_example import Service
|
||||||
|
from ioc_example import Client
|
||||||
|
|
||||||
|
|
||||||
class Components(catalogs.DeclarativeCatalog):
|
class Components(catalogs.DeclarativeCatalog):
|
Loading…
Reference in New Issue
Block a user