From 2cd80b1b4c6124fe2f1a9cc7d5197802d086d7a3 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 1 Apr 2016 10:47:09 +0300 Subject: [PATCH] Update What Is DI docs and example --- docs/main/introduction/what_is_di.rst | 15 ++++++++------- examples/ioc_di_demos/car_engine_1.py | 18 ++++++++++++++++++ examples/ioc_di_demos/car_engine_2.py | 18 ++++++++++++++++++ examples/{di_demo => ioc_di_demos}/example.py | 0 .../ioc_container_example.py} | 9 ++++++--- .../{di_demo => ioc_di_demos}/ioc_example.py | 0 6 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 examples/ioc_di_demos/car_engine_1.py create mode 100644 examples/ioc_di_demos/car_engine_2.py rename examples/{di_demo => ioc_di_demos}/example.py (100%) rename examples/{di_demo/di_example.py => ioc_di_demos/ioc_container_example.py} (59%) rename examples/{di_demo => ioc_di_demos}/ioc_example.py (100%) diff --git a/docs/main/introduction/what_is_di.rst b/docs/main/introduction/what_is_di.rst index 02c7909a..bf57b70f 100644 --- a/docs/main/introduction/what_is_di.rst +++ b/docs/main/introduction/what_is_di.rst @@ -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. diff --git a/examples/ioc_di_demos/car_engine_1.py b/examples/ioc_di_demos/car_engine_1.py new file mode 100644 index 00000000..bf6fde9a --- /dev/null +++ b/examples/ioc_di_demos/car_engine_1.py @@ -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 diff --git a/examples/ioc_di_demos/car_engine_2.py b/examples/ioc_di_demos/car_engine_2.py new file mode 100644 index 00000000..194506d0 --- /dev/null +++ b/examples/ioc_di_demos/car_engine_2.py @@ -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 diff --git a/examples/di_demo/example.py b/examples/ioc_di_demos/example.py similarity index 100% rename from examples/di_demo/example.py rename to examples/ioc_di_demos/example.py diff --git a/examples/di_demo/di_example.py b/examples/ioc_di_demos/ioc_container_example.py similarity index 59% rename from examples/di_demo/di_example.py rename to examples/ioc_di_demos/ioc_container_example.py index 211ed658..df0032e1 100644 --- a/examples/di_demo/di_example.py +++ b/examples/ioc_di_demos/ioc_container_example.py @@ -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): diff --git a/examples/di_demo/ioc_example.py b/examples/ioc_di_demos/ioc_example.py similarity index 100% rename from examples/di_demo/ioc_example.py rename to examples/ioc_di_demos/ioc_example.py