Update What Is DI docs and example

This commit is contained in:
Roman Mogilatov 2016-04-01 10:47:09 +03:00
parent c8115ed44e
commit 2cd80b1b4c
6 changed files with 50 additions and 10 deletions

View File

@ -74,25 +74,26 @@ Example
Let's go through the code of ``example.py``:
.. literalinclude:: ../../../examples/di_demo/example.py
.. literalinclude:: ../../../examples/ioc_di_demos/example.py
:language: python
At some point, things defined above mean, that the code from ``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
Also the code from ``ioc_example.py`` could be powered by dependency
injection framework, like in ``di_example.py``:
Also the code from ``ioc_example.py`` could be upgraded with inversion of
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
.. note::
``Components`` from ``di_example.py`` is an IoC container. It contains a
collection of component providers that could be injected into each other.
``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.

View 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

View 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

View File

@ -1,7 +1,10 @@
"""The Code, powered by Dependency Injector."""
"""The Code, that uses IoC container."""
from dependency_injector import catalogs, providers
from ioc_example import Service, Client
from dependency_injector import catalogs
from dependency_injector import providers
from ioc_example import Service
from ioc_example import Client
class Components(catalogs.DeclarativeCatalog):