mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Add new multiple containers example
This commit is contained in:
parent
990fd3a554
commit
2dc78a6875
|
@ -7,6 +7,14 @@ that were made in every particular version.
|
||||||
From version 0.7.6 *Dependency Injector* framework strictly
|
From version 0.7.6 *Dependency Injector* framework strictly
|
||||||
follows `Semantic versioning`_
|
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 <https://github.com/ets-labs/python-dependency-injector/issues/207>`_.
|
||||||
|
- Add attributes forwarding for the ``Dependency`` provider.
|
||||||
|
|
||||||
4.24.0
|
4.24.0
|
||||||
------
|
------
|
||||||
- Add docs on ``@containers.copy()`` decorator.
|
- Add docs on ``@containers.copy()`` decorator.
|
||||||
|
|
|
@ -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]
|
|
@ -0,0 +1 @@
|
||||||
|
"""Top-level package."""
|
|
@ -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]
|
|
@ -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)
|
|
@ -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'}})
|
Loading…
Reference in New Issue
Block a user