mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Add example
This commit is contained in:
parent
a72f3c5b6c
commit
c15fc27f00
45
examples/providers/factory_aggregate_non_string_keys.py
Normal file
45
examples/providers/factory_aggregate_non_string_keys.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
"""`FactoryAggregate` provider with non-string keys example."""
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
|
||||
|
||||
class Command:
|
||||
...
|
||||
|
||||
|
||||
class CommandA(Command):
|
||||
...
|
||||
|
||||
|
||||
class CommandB(Command):
|
||||
...
|
||||
|
||||
|
||||
class Handler:
|
||||
...
|
||||
|
||||
|
||||
class HandlerA(Handler):
|
||||
...
|
||||
|
||||
|
||||
class HandlerB(Handler):
|
||||
...
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
handler_factory = providers.FactoryAggregate({
|
||||
CommandA: providers.Factory(HandlerA),
|
||||
CommandB: providers.Factory(HandlerB),
|
||||
})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
container = Container()
|
||||
|
||||
handler_a = container.handler_factory(CommandA)
|
||||
handler_b = container.handler_factory(CommandB)
|
||||
|
||||
assert isinstance(handler_a, HandlerA)
|
||||
assert isinstance(handler_b, HandlerB)
|
Loading…
Reference in New Issue
Block a user