diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index ec883dc9..f3c752f5 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -7,6 +7,14 @@ that were made in every particular version. From version 0.7.6 *Dependency Injector* framework strictly follows `Semantic versioning`_ +Development version +------------------- +- Add ``application-multiple-containers-runtime-overriding`` example. This example demonstrates + how to build application from multiple containers and override one container config from + another one in the runtime. + See issue: `#207 `_. +- Add attributes forwarding for the ``Dependency`` provider. + 4.24.0 ------ - Add docs on ``@containers.copy()`` decorator. diff --git a/examples/miniapps/application-multiple-containers-runtime-overriding/README.rst b/examples/miniapps/application-multiple-containers-runtime-overriding/README.rst new file mode 100644 index 00000000..991f183f --- /dev/null +++ b/examples/miniapps/application-multiple-containers-runtime-overriding/README.rst @@ -0,0 +1,17 @@ +Application example (multiple containers, runtime config overriding) +==================================================================== + +This example demonstrates how to build application from multiple containers +and override one container config from another one in the runtime. + +Run: + +.. code-block:: bash + + python -m example + +You should see: + +.. code-block:: bash + + Adapter=[DB Path=[~/test]], queue=[Some storage] diff --git a/examples/miniapps/application-multiple-containers-runtime-overriding/example/__init__.py b/examples/miniapps/application-multiple-containers-runtime-overriding/example/__init__.py new file mode 100644 index 00000000..1c744ca5 --- /dev/null +++ b/examples/miniapps/application-multiple-containers-runtime-overriding/example/__init__.py @@ -0,0 +1 @@ +"""Top-level package.""" diff --git a/examples/miniapps/application-multiple-containers-runtime-overriding/example/__main__.py b/examples/miniapps/application-multiple-containers-runtime-overriding/example/__main__.py new file mode 100644 index 00000000..2175884b --- /dev/null +++ b/examples/miniapps/application-multiple-containers-runtime-overriding/example/__main__.py @@ -0,0 +1,12 @@ +"""Main module.""" + +from .containers import Application + + +if __name__ == '__main__': + application = Application() + config = application.service.config() + config.build() + + print(application.repository.site()) + # Output: Adapter=[DB Path=[~/test]], queue=[Some storage] diff --git a/examples/miniapps/application-multiple-containers-runtime-overriding/example/containers.py b/examples/miniapps/application-multiple-containers-runtime-overriding/example/containers.py new file mode 100644 index 00000000..35ec4035 --- /dev/null +++ b/examples/miniapps/application-multiple-containers-runtime-overriding/example/containers.py @@ -0,0 +1,45 @@ +"""Containers module.""" + +from dependency_injector import containers, providers + +from .services import ConfigService + + +class Core(containers.DeclarativeContainer): + config = providers.Configuration('config') + + +class Storage(containers.DeclarativeContainer): + queue = providers.Singleton(lambda: 'Some storage') + + +class Adapter(containers.DeclarativeContainer): + core = providers.DependenciesContainer(config=providers.Configuration()) + tinydb = providers.Singleton( + lambda db_path: f'DB Path=[{db_path}]', + db_path=core.config.default.db_path, + ) + + +class Repository(containers.DeclarativeContainer): + adapter = providers.DependenciesContainer() + storage = providers.DependenciesContainer() + site = providers.Singleton( + lambda adapter, queue: f'Adapter=[{adapter}], queue=[{queue}]', + adapter=adapter.tinydb, + queue=storage.queue, + ) + + +class Service(containers.DeclarativeContainer): + core = providers.DependenciesContainer() + config = providers.Singleton(ConfigService, core.config.provider) + + +class Application(containers.DeclarativeContainer): + + core = providers.Container(Core) + storage = providers.Container(Storage) + adapter = providers.Container(Adapter, core=core) + repository = providers.Container(Repository, adapter=adapter, storage=storage) + service = providers.Container(Service, core=core) diff --git a/examples/miniapps/application-multiple-containers-runtime-overriding/example/services.py b/examples/miniapps/application-multiple-containers-runtime-overriding/example/services.py new file mode 100644 index 00000000..e0901e13 --- /dev/null +++ b/examples/miniapps/application-multiple-containers-runtime-overriding/example/services.py @@ -0,0 +1,9 @@ +"""Services module.""" + + +class ConfigService: + def __init__(self, config): + self._config = config + + def build(self): + self._config.from_dict({'default': {'db_path': '~/test'}})