mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-22 21:46:17 +03:00
Add Selector provider example
This commit is contained in:
parent
a4b38f9c83
commit
405358bd04
28
examples/providers/selector.py
Normal file
28
examples/providers/selector.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
"""`Selector` provider example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
|
||||
|
||||
class SomeClass:
|
||||
...
|
||||
|
||||
|
||||
class SomeOtherClass:
|
||||
...
|
||||
|
||||
|
||||
config = providers.Configuration()
|
||||
|
||||
selector = providers.Selector(
|
||||
config.one_or_another,
|
||||
one=providers.Factory(SomeClass),
|
||||
another=providers.Factory(SomeOtherClass),
|
||||
)
|
||||
|
||||
config.override({'one_or_another': 'one'})
|
||||
some_instance_1 = selector()
|
||||
assert isinstance(some_instance_1, SomeClass)
|
||||
|
||||
config.override({'one_or_another': 'another'})
|
||||
some_instance_2 = selector()
|
||||
assert isinstance(some_instance_2, SomeOtherClass)
|
Loading…
Reference in New Issue
Block a user