Fetching contributors…
Cannot retrieve contributors at this time
78 lines (52 sloc) 2.09 KB

Bundles mini application example

.. currentmodule:: dependency_injector.containers

"Bundles" is an example mini application that is intented to demonstrate the power of dependency injection for creation of re-usable application components ("bundles") with 100% transparency of their dependencies.

Example application

"Bundles" mini application has next structure:

    bundles/               <-- Bundles package
        photos/            <-- Photos bundle
      <-- Photos bundle dependency injection container
        users/             <-- Users bundle
      <-- Users bundle dependency injection container
                   <-- Entrypoint

IoC containers

Next two listings show :py:class:`DeclarativeContainer`'s for "users" and "photos" bundles.

Listing of bundeles/users/

.. literalinclude:: ../../examples/miniapps/bundles/bundles/users/
   :language: python


  • Users container has dependency on database.

Listing of bundeles/photos/

.. literalinclude:: ../../examples/miniapps/bundles/bundles/photos/
   :language: python


  • Photos container has dependencies on database and file storage.

Run application

Finally, both "bundles" are initialized by providing needed dependencies. Initialization of dependencies happens right in the runtime, not earlier. Generally, it means, that any part of any bundle could be overridden on the fly.

Listing of

.. literalinclude:: ../../examples/miniapps/bundles/
   :language: python


.. disqus::