Add bundles example miniapp

This commit is contained in:
Roman Mogylatov 2017-12-25 16:47:58 +02:00
parent 4a24549923
commit d5ac1474d4
4 changed files with 95 additions and 6 deletions

View File

@ -0,0 +1,77 @@
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:
.. code-block:: bash
bundles/
bundles/ <-- Bundles package
photos/ <-- Photos bundle
__init__.py <-- Photos bundle dependency injection container
entities.py
repositories.py
users/ <-- Users bundle
__init__.py <-- Users bundle dependency injection container
entities.py
repositories.py
run.py <-- Entrypoint
IoC containers
~~~~~~~~~~~~~~
Next two listings show :py:class:`DeclarativeContainer`'s for "users" and
"photos" bundles.
Listing of ``bundeles/users/__init__.py``:
.. literalinclude:: ../../examples/miniapps/bundles/bundles/users/__init__.py
:language: python
:linenos:
.. note::
- ``Users`` container has dependency on database.
Listing of ``bundeles/photos/__init__.py``:
.. literalinclude:: ../../examples/miniapps/bundles/bundles/photos/__init__.py
:language: python
:linenos:
.. note::
- ``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 ``run.py``:
.. literalinclude:: ../../examples/miniapps/bundles/run.py
:language: python
:linenos:
Links
~~~~~
+ `Dependency Injector <https://github.com/ets-labs/python-dependency-injector/>`_
+ `Full example sources <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/bundles>`_
.. disqus::

View File

@ -17,3 +17,4 @@ and powered by *Dependency Injector* framework.
movie_lister
services_miniapp
bundles_miniapp

View File

@ -20,6 +20,7 @@ Development version
- Add method to dynamic catalog for overriding groups of providers -
``DynamicContainer.set_providers(**overriding_providers)``.
- Fix bug when copying ``Configuration`` provider.
- Add "bundles" example miniapp.
3.8.2

View File

@ -1,4 +1,7 @@
"""Example application - Bundles."""
"""Run 'Bundles' example application."""
import sqlite3
import boto3
from dependency_injector import containers
from dependency_injector import providers
@ -10,15 +13,22 @@ from bundles.photos import Photos
class Core(containers.DeclarativeContainer):
"""Core container."""
pgsql = providers.Singleton(object)
s3 = providers.Singleton(object)
config = providers.Configuration('config')
sqlite = providers.Singleton(sqlite3.connect, config.database.dsn)
s3 = providers.Singleton(
boto3.client, 's3',
aws_access_key_id=config.aws.access_key_id,
aws_secret_access_key=config.aws.secret_access_key)
if __name__ == '__main__':
# Initializing containers
core = Core()
users = Users(database=core.pgsql)
photos = Photos(database=core.pgsql, file_storage=core.s3)
core.config.update({'database': {'dsn': ':memory:'},
'aws': {'access_key_id': 'KEY',
'secret_access_key': 'SECRET'}})
users = Users(database=core.sqlite)
photos = Photos(database=core.sqlite, file_storage=core.s3)
# Fetching few users
user_repository = users.user_repository()
@ -28,4 +38,4 @@ if __name__ == '__main__':
# Making some checks
assert user1.id == 1
assert user2.id == 2
assert user_repository.db is core.pgsql()
assert user_repository.db is core.sqlite()