2022-01-10 03:21:31 +03:00
|
|
|
.. _aggregate-provider:
|
|
|
|
|
|
|
|
Aggregate provider
|
|
|
|
==================
|
|
|
|
|
|
|
|
.. meta::
|
|
|
|
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Configuration,Injection,
|
|
|
|
Aggregate,Polymorphism,Environment Variable,Flexibility
|
|
|
|
:description: Aggregate provider aggregates other providers.
|
|
|
|
This page demonstrates how to implement the polymorphism and increase the
|
|
|
|
flexibility of your application using the Aggregate provider.
|
|
|
|
|
2022-01-10 05:40:50 +03:00
|
|
|
:py:class:`Aggregate` provider aggregates a group of other providers.
|
2022-01-10 03:21:31 +03:00
|
|
|
|
|
|
|
.. currentmodule:: dependency_injector.providers
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/aggregate.py
|
|
|
|
:language: python
|
|
|
|
:lines: 3-
|
|
|
|
:emphasize-lines: 24-27
|
|
|
|
|
|
|
|
Each provider in the ``Aggregate`` is associated with a key. You can call aggregated providers by providing
|
2022-01-10 05:40:50 +03:00
|
|
|
their key as a first argument. All positional and keyword arguments following the key will be forwarded to
|
2022-01-10 03:21:31 +03:00
|
|
|
the called provider:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
yaml_reader = container.config_readers("yaml", "./config.yml", foo=...)
|
|
|
|
|
|
|
|
You can also retrieve an aggregated provider by providing its key as an attribute name:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
yaml_reader = container.config_readers.yaml("./config.yml", foo=...)
|
|
|
|
|
2022-01-10 05:40:50 +03:00
|
|
|
To retrieve a dictionary of aggregated providers, use ``.providers`` attribute:
|
2022-01-10 03:21:31 +03:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
container.config_readers.providers == {
|
|
|
|
"yaml": <YAML provider>,
|
|
|
|
"json": <JSON provider>,
|
|
|
|
}
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
You can not override the ``Aggregate`` provider.
|
|
|
|
|
|
|
|
.. note::
|
2022-01-10 05:40:50 +03:00
|
|
|
When you inject the ``Aggregate`` provider, it is passed "as is".
|
2022-01-10 03:21:31 +03:00
|
|
|
|
|
|
|
To use non-string keys or string keys with ``.`` and ``-``, provide a dictionary as a positional argument:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
aggregate = providers.Aggregate({
|
|
|
|
SomeClass: providers.Factory(...),
|
|
|
|
"key.with.periods": providers.Factory(...),
|
|
|
|
"key-with-dashes": providers.Factory(...),
|
|
|
|
})
|
|
|
|
|
2022-01-10 05:25:52 +03:00
|
|
|
.. seealso::
|
2022-01-10 05:40:50 +03:00
|
|
|
:ref:`selector-provider` to make injections based on a configuration value, environment variable, or a result of a callable.
|
2022-01-10 03:21:31 +03:00
|
|
|
|
2022-01-10 05:25:52 +03:00
|
|
|
``Aggregate`` provider is different from the :ref:`selector-provider`. ``Aggregate`` provider doesn't select which provider
|
2022-01-10 05:40:50 +03:00
|
|
|
to inject and doesn't have a selector. It is a group of providers and is always injected "as is". The rest of the interface
|
|
|
|
of both providers is similar.
|
2022-01-10 03:21:31 +03:00
|
|
|
|
2022-01-10 05:25:52 +03:00
|
|
|
.. note::
|
|
|
|
``Aggregate`` provider is a successor of :ref:`factory-aggregate-provider` provider. ``Aggregate`` provider doesn't have
|
2022-01-10 05:40:50 +03:00
|
|
|
a restriction on the provider type, while ``FactoryAggregate`` aggregates only ``Factory`` providers.
|
2022-01-10 03:21:31 +03:00
|
|
|
|
|
|
|
.. disqus::
|