Add container usage for list provider example

This commit is contained in:
Roman Mogylatov 2020-09-03 17:03:28 -04:00
parent 6528411271
commit 2a23f3d2f4
2 changed files with 15 additions and 11 deletions

View File

@ -13,7 +13,7 @@ List provider
.. literalinclude:: ../../examples/providers/list.py
:language: python
:lines: 3-
:emphasize-lines: 19-22
:emphasize-lines: 21-24
``List`` provider handles positional arguments the same way as a :ref:`factory-provider`.

View File

@ -3,7 +3,7 @@
import dataclasses
from typing import List
from dependency_injector import providers
from dependency_injector import containers, providers
@dataclasses.dataclass
@ -16,23 +16,27 @@ class Dispatcher:
modules: List[Module]
dispatcher_factory = providers.Factory(
Dispatcher,
modules=providers.List(
providers.Factory(Module, name='m1'),
providers.Factory(Module, name='m2'),
),
)
class Container(containers.DeclarativeContainer):
dispatcher_factory = providers.Factory(
Dispatcher,
modules=providers.List(
providers.Factory(Module, name='m1'),
providers.Factory(Module, name='m2'),
),
)
if __name__ == '__main__':
dispatcher = dispatcher_factory()
container = Container()
dispatcher = container.dispatcher_factory()
assert isinstance(dispatcher.modules, list)
assert dispatcher.modules[0].name == 'm1'
assert dispatcher.modules[1].name == 'm2'
# Call "dispatcher = dispatcher_factory()" is an equivalent for:
# Call "dispatcher = container.dispatcher_factory()" is equivalent to:
# dispatcher = Dispatcher(
# modules=[
# Module(name='m1'),