|
| 1 | +Bundles mini application example |
| 2 | +-------------------------------- |
| 3 | + |
| 4 | +.. currentmodule:: dependency_injector.containers |
| 5 | + |
| 6 | +"Bundles" is an example mini application that is intented to demonstrate the |
| 7 | +power of dependency injection for creation of re-usable application components |
| 8 | +("bundles") with 100% transparency of their dependencies. |
| 9 | + |
| 10 | +Example application |
| 11 | +~~~~~~~~~~~~~~~~~~~ |
| 12 | + |
| 13 | +"Bundles" mini application has next structure: |
| 14 | + |
| 15 | +.. code-block:: bash |
| 16 | +
|
| 17 | + bundles/ |
| 18 | + bundles/ <-- Bundles package |
| 19 | + photos/ <-- Photos bundle |
| 20 | + __init__.py <-- Photos bundle dependency injection container |
| 21 | + entities.py |
| 22 | + repositories.py |
| 23 | + users/ <-- Users bundle |
| 24 | + __init__.py <-- Users bundle dependency injection container |
| 25 | + entities.py |
| 26 | + repositories.py |
| 27 | + run.py <-- Entrypoint |
| 28 | +
|
| 29 | +
|
| 30 | +IoC containers |
| 31 | +~~~~~~~~~~~~~~ |
| 32 | + |
| 33 | +Next two listings show :py:class:`DeclarativeContainer`'s for "users" and |
| 34 | +"photos" bundles. |
| 35 | + |
| 36 | +Listing of ``bundeles/users/__init__.py``: |
| 37 | + |
| 38 | +.. literalinclude:: ../../examples/miniapps/bundles/bundles/users/__init__.py |
| 39 | + :language: python |
| 40 | + :linenos: |
| 41 | + |
| 42 | +.. note:: |
| 43 | + |
| 44 | + - ``Users`` container has dependency on database. |
| 45 | + |
| 46 | +Listing of ``bundeles/photos/__init__.py``: |
| 47 | + |
| 48 | +.. literalinclude:: ../../examples/miniapps/bundles/bundles/photos/__init__.py |
| 49 | + :language: python |
| 50 | + :linenos: |
| 51 | + |
| 52 | +.. note:: |
| 53 | + |
| 54 | + - ``Photos`` container has dependencies on database and file storage. |
| 55 | + |
| 56 | +Run application |
| 57 | +~~~~~~~~~~~~~~~ |
| 58 | + |
| 59 | +Finally, both "bundles" are initialized by providing needed dependencies. |
| 60 | +Initialization of dependencies happens right in the runtime, not earlier. |
| 61 | +Generally, it means, that any part of any bundle could be overridden on the |
| 62 | +fly. |
| 63 | + |
| 64 | +Listing of ``run.py``: |
| 65 | + |
| 66 | +.. literalinclude:: ../../examples/miniapps/bundles/run.py |
| 67 | + :language: python |
| 68 | + :linenos: |
| 69 | + |
| 70 | +Links |
| 71 | +~~~~~ |
| 72 | + |
| 73 | ++ `Dependency Injector <https://github.com/ets-labs/python-dependency-injector/>`_ |
| 74 | ++ `Full example sources <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/bundles>`_ |
| 75 | + |
| 76 | + |
| 77 | +.. disqus:: |
0 commit comments