From a8c9219d093c83adf60035b2387027cc93569c46 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Wed, 19 Oct 2016 19:28:49 +0300 Subject: [PATCH] Update dependency injection example in introduction/what_is_di.rst --- docs/introduction/what_is_di.rst | 6 +++--- examples/{ioc_di_demos => di_demo}/example.py | 0 .../example_ioc.py => di_demo/example_di.py} | 2 +- examples/ioc_di_demos/car_engine.py | 17 ----------------- examples/ioc_di_demos/car_engine_ioc.py | 18 ------------------ .../ioc_di_demos/car_engine_ioc_container.py | 19 ------------------- 6 files changed, 4 insertions(+), 58 deletions(-) rename examples/{ioc_di_demos => di_demo}/example.py (100%) rename examples/{ioc_di_demos/example_ioc.py => di_demo/example_di.py} (87%) delete mode 100644 examples/ioc_di_demos/car_engine.py delete mode 100644 examples/ioc_di_demos/car_engine_ioc.py delete mode 100644 examples/ioc_di_demos/car_engine_ioc_container.py diff --git a/docs/introduction/what_is_di.rst b/docs/introduction/what_is_di.rst index fb77865c..0f1cf595 100644 --- a/docs/introduction/what_is_di.rst +++ b/docs/introduction/what_is_di.rst @@ -81,14 +81,14 @@ Example Let's go through the code of ``example.py``: -.. literalinclude:: ../../examples/ioc_di_demos/example.py +.. literalinclude:: ../../examples/di_demo/example.py :language: python :linenos: At some point, things defined above mean, that the code from ``example.py``, -could look different, like in ``example_ioc.py``: +could look different, like in ``example_di.py``: -.. literalinclude:: ../../examples/ioc_di_demos/example_ioc.py +.. literalinclude:: ../../examples/di_demo/example_di.py :language: python :linenos: diff --git a/examples/ioc_di_demos/example.py b/examples/di_demo/example.py similarity index 100% rename from examples/ioc_di_demos/example.py rename to examples/di_demo/example.py diff --git a/examples/ioc_di_demos/example_ioc.py b/examples/di_demo/example_di.py similarity index 87% rename from examples/ioc_di_demos/example_ioc.py rename to examples/di_demo/example_di.py index 7166250f..cb289a19 100644 --- a/examples/ioc_di_demos/example_ioc.py +++ b/examples/di_demo/example_di.py @@ -1,4 +1,4 @@ -"""The Code, that follows inversion of control principle.""" +"""The Code, that demonstrates dependency injection pattern.""" class Service(object): diff --git a/examples/ioc_di_demos/car_engine.py b/examples/ioc_di_demos/car_engine.py deleted file mode 100644 index fc09c16d..00000000 --- a/examples/ioc_di_demos/car_engine.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Car & Engine example.""" - - -class Engine(object): - """Example engine.""" - - -class Car(object): - """Example car.""" - - def __init__(self): - """Initializer.""" - self.engine = Engine() # Engine is a "hardcoded" dependency - - -if __name__ == '__main__': - car = Car() # Application creates Car's instance diff --git a/examples/ioc_di_demos/car_engine_ioc.py b/examples/ioc_di_demos/car_engine_ioc.py deleted file mode 100644 index 83b26f90..00000000 --- a/examples/ioc_di_demos/car_engine_ioc.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Refactored Car & Engine example that demonstrates inversion of control.""" - - -class Engine(object): - """Example engine.""" - - -class Car(object): - """Example car.""" - - def __init__(self, engine): - """Initializer.""" - self.engine = engine # Engine is an "injected" dependency - - -if __name__ == '__main__': - engine = Engine() # Application creates Engine's instance - car = Car(engine) # and inject it into the Car's instance diff --git a/examples/ioc_di_demos/car_engine_ioc_container.py b/examples/ioc_di_demos/car_engine_ioc_container.py deleted file mode 100644 index e01efddd..00000000 --- a/examples/ioc_di_demos/car_engine_ioc_container.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Example of inversion of control container for Car & Engine example.""" - -import dependency_injector.containers as containers -import dependency_injector.providers as providers - -import car_engine_ioc - - -class Container(containers.DeclarativeContainer): - """IoC container of component providers.""" - - engine = providers.Factory(car_engine_ioc.Engine) - - car = providers.Factory(car_engine_ioc.Car, - engine=engine) - - -if __name__ == '__main__': - car = Container.car() # Application creates Car's instance