mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-31 03:36:41 +03:00
Add container usage for list provider example
This commit is contained in:
parent
6528411271
commit
2a23f3d2f4
|
@ -13,7 +13,7 @@ List provider
|
||||||
.. literalinclude:: ../../examples/providers/list.py
|
.. literalinclude:: ../../examples/providers/list.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-
|
:lines: 3-
|
||||||
:emphasize-lines: 19-22
|
:emphasize-lines: 21-24
|
||||||
|
|
||||||
``List`` provider handles positional arguments the same way as a :ref:`factory-provider`.
|
``List`` provider handles positional arguments the same way as a :ref:`factory-provider`.
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
import dataclasses
|
import dataclasses
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
|
@ -16,23 +16,27 @@ class Dispatcher:
|
||||||
modules: List[Module]
|
modules: List[Module]
|
||||||
|
|
||||||
|
|
||||||
dispatcher_factory = providers.Factory(
|
class Container(containers.DeclarativeContainer):
|
||||||
Dispatcher,
|
|
||||||
modules=providers.List(
|
dispatcher_factory = providers.Factory(
|
||||||
providers.Factory(Module, name='m1'),
|
Dispatcher,
|
||||||
providers.Factory(Module, name='m2'),
|
modules=providers.List(
|
||||||
),
|
providers.Factory(Module, name='m1'),
|
||||||
)
|
providers.Factory(Module, name='m2'),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
dispatcher = dispatcher_factory()
|
container = Container()
|
||||||
|
|
||||||
|
dispatcher = container.dispatcher_factory()
|
||||||
|
|
||||||
assert isinstance(dispatcher.modules, list)
|
assert isinstance(dispatcher.modules, list)
|
||||||
assert dispatcher.modules[0].name == 'm1'
|
assert dispatcher.modules[0].name == 'm1'
|
||||||
assert dispatcher.modules[1].name == 'm2'
|
assert dispatcher.modules[1].name == 'm2'
|
||||||
|
|
||||||
# Call "dispatcher = dispatcher_factory()" is an equivalent for:
|
# Call "dispatcher = container.dispatcher_factory()" is equivalent to:
|
||||||
# dispatcher = Dispatcher(
|
# dispatcher = Dispatcher(
|
||||||
# modules=[
|
# modules=[
|
||||||
# Module(name='m1'),
|
# Module(name='m1'),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user