|
| 1 | +.. _aggregate-provider: |
| 2 | + |
| 3 | +Aggregate provider |
| 4 | +================== |
| 5 | + |
| 6 | +.. meta:: |
| 7 | + :keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Configuration,Injection, |
| 8 | + Aggregate,Polymorphism,Environment Variable,Flexibility |
| 9 | + :description: Aggregate provider aggregates other providers. |
| 10 | + This page demonstrates how to implement the polymorphism and increase the |
| 11 | + flexibility of your application using the Aggregate provider. |
| 12 | + |
| 13 | +:py:class:`Aggregate` provider aggregates a group of other providers. |
| 14 | + |
| 15 | +.. currentmodule:: dependency_injector.providers |
| 16 | + |
| 17 | +.. literalinclude:: ../../examples/providers/aggregate.py |
| 18 | + :language: python |
| 19 | + :lines: 3- |
| 20 | + :emphasize-lines: 24-27 |
| 21 | + |
| 22 | +Each provider in the ``Aggregate`` is associated with a key. You can call aggregated providers by providing |
| 23 | +their key as a first argument. All positional and keyword arguments following the key will be forwarded to |
| 24 | +the called provider: |
| 25 | + |
| 26 | +.. code-block:: python |
| 27 | +
|
| 28 | + yaml_reader = container.config_readers("yaml", "./config.yml", foo=...) |
| 29 | +
|
| 30 | +You can also retrieve an aggregated provider by providing its key as an attribute name: |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + yaml_reader = container.config_readers.yaml("./config.yml", foo=...) |
| 35 | +
|
| 36 | +To retrieve a dictionary of aggregated providers, use ``.providers`` attribute: |
| 37 | + |
| 38 | +.. code-block:: python |
| 39 | +
|
| 40 | + container.config_readers.providers == { |
| 41 | + "yaml": <YAML provider>, |
| 42 | + "json": <JSON provider>, |
| 43 | + } |
| 44 | +
|
| 45 | +.. note:: |
| 46 | + You can not override the ``Aggregate`` provider. |
| 47 | + |
| 48 | +.. note:: |
| 49 | + When you inject the ``Aggregate`` provider, it is passed "as is". |
| 50 | + |
| 51 | +To use non-string keys or string keys with ``.`` and ``-``, provide a dictionary as a positional argument: |
| 52 | + |
| 53 | +.. code-block:: python |
| 54 | +
|
| 55 | + aggregate = providers.Aggregate({ |
| 56 | + SomeClass: providers.Factory(...), |
| 57 | + "key.with.periods": providers.Factory(...), |
| 58 | + "key-with-dashes": providers.Factory(...), |
| 59 | + }) |
| 60 | +
|
| 61 | +.. seealso:: |
| 62 | + :ref:`selector-provider` to make injections based on a configuration value, environment variable, or a result of a callable. |
| 63 | + |
| 64 | + ``Aggregate`` provider is different from the :ref:`selector-provider`. ``Aggregate`` provider doesn't select which provider |
| 65 | + 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 |
| 66 | + of both providers is similar. |
| 67 | + |
| 68 | +.. note:: |
| 69 | + ``Aggregate`` provider is a successor of :ref:`factory-aggregate-provider` provider. ``Aggregate`` provider doesn't have |
| 70 | + a restriction on the provider type, while ``FactoryAggregate`` aggregates only ``Factory`` providers. |
| 71 | + |
| 72 | +.. disqus:: |
0 commit comments