Skip to content

Commit d5ac147

Browse files
committed
Add bundles example miniapp
1 parent 4a24549 commit d5ac147

File tree

4 files changed

+95
-6
lines changed

4 files changed

+95
-6
lines changed

docs/examples/bundles_miniapp.rst

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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::

docs/examples/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ and powered by *Dependency Injector* framework.
1717

1818
movie_lister
1919
services_miniapp
20+
bundles_miniapp

docs/main/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Development version
2020
- Add method to dynamic catalog for overriding groups of providers -
2121
``DynamicContainer.set_providers(**overriding_providers)``.
2222
- Fix bug when copying ``Configuration`` provider.
23+
- Add "bundles" example miniapp.
2324

2425

2526
3.8.2

examples/miniapps/bundles/run.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
"""Example application - Bundles."""
1+
"""Run 'Bundles' example application."""
2+
3+
import sqlite3
4+
import boto3
25

36
from dependency_injector import containers
47
from dependency_injector import providers
@@ -10,15 +13,22 @@
1013
class Core(containers.DeclarativeContainer):
1114
"""Core container."""
1215

13-
pgsql = providers.Singleton(object)
14-
s3 = providers.Singleton(object)
16+
config = providers.Configuration('config')
17+
sqlite = providers.Singleton(sqlite3.connect, config.database.dsn)
18+
s3 = providers.Singleton(
19+
boto3.client, 's3',
20+
aws_access_key_id=config.aws.access_key_id,
21+
aws_secret_access_key=config.aws.secret_access_key)
1522

1623

1724
if __name__ == '__main__':
1825
# Initializing containers
1926
core = Core()
20-
users = Users(database=core.pgsql)
21-
photos = Photos(database=core.pgsql, file_storage=core.s3)
27+
core.config.update({'database': {'dsn': ':memory:'},
28+
'aws': {'access_key_id': 'KEY',
29+
'secret_access_key': 'SECRET'}})
30+
users = Users(database=core.sqlite)
31+
photos = Photos(database=core.sqlite, file_storage=core.s3)
2232

2333
# Fetching few users
2434
user_repository = users.user_repository()
@@ -28,4 +38,4 @@ class Core(containers.DeclarativeContainer):
2838
# Making some checks
2939
assert user1.id == 1
3040
assert user2.id == 2
31-
assert user_repository.db is core.pgsql()
41+
assert user_repository.db is core.sqlite()

0 commit comments

Comments
 (0)