Add usage of the container to the selector example

This commit is contained in:
Roman Mogylatov 2020-09-03 17:38:52 -04:00
parent e48746d65f
commit d61281a0b9
2 changed files with 17 additions and 12 deletions

View File

@ -17,7 +17,7 @@ Selector provider
.. literalinclude:: ../../examples/providers/selector.py
:language: python
:lines: 3-
:emphasize-lines: 14-18
:emphasize-lines: 16-20
The first argument of the ``Selector`` provider is called ``selector``. It can be an option of
a ``Configuration`` provider or any other callable. The ``selector`` callable has to return a

View File

@ -1,6 +1,6 @@
"""`Selector` provider example."""
from dependency_injector import providers
from dependency_injector import containers, providers
class SomeClass:
@ -11,19 +11,24 @@ class SomeOtherClass:
...
config = providers.Configuration()
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
selector = providers.Selector(
config.one_or_another,
one=providers.Factory(SomeClass),
another=providers.Factory(SomeOtherClass),
)
selector = providers.Selector(
config.one_or_another,
one=providers.Factory(SomeClass),
another=providers.Factory(SomeOtherClass),
)
if __name__ == '__main__':
config.override({'one_or_another': 'one'})
instance_1 = selector()
container = Container()
container.config.override({'one_or_another': 'one'})
instance_1 = container.selector()
assert isinstance(instance_1, SomeClass)
config.override({'one_or_another': 'another'})
instance_2 = selector()
container.config.override({'one_or_another': 'another'})
instance_2 = container.selector()
assert isinstance(instance_2, SomeOtherClass)