2016-05-29 22:06:02 +03:00
|
|
|
"""Declarative IoC container overriding example."""
|
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
import dependency_injector.containers as containers
|
|
|
|
import dependency_injector.providers as providers
|
2016-05-29 22:06:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
2016-06-06 11:26:53 +03:00
|
|
|
"""IoC container."""
|
2016-05-29 22:06:02 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
sequence_factory = providers.Factory(list)
|
2016-05-29 22:06:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
class OverridingContainer(containers.DeclarativeContainer):
|
|
|
|
"""Overriding IoC container."""
|
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
sequence_factory = providers.Factory(tuple)
|
2016-05-29 22:06:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Overriding `Container` with `OverridingContainer`:
|
|
|
|
Container.override(OverridingContainer)
|
|
|
|
|
|
|
|
# Creating some objects using overridden container:
|
2016-06-06 11:26:53 +03:00
|
|
|
sequence_1 = Container.sequence_factory([1, 2, 3])
|
|
|
|
sequence_2 = Container.sequence_factory([3, 2, 1])
|
2016-05-29 22:06:02 +03:00
|
|
|
|
|
|
|
# Making some asserts:
|
2016-06-06 11:26:53 +03:00
|
|
|
assert Container.overridden == (OverridingContainer,)
|
|
|
|
assert sequence_1 == (1, 2, 3) and sequence_2 == (3, 2, 1)
|