mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 21:14:00 +03:00
1eee0fe529
+ Refactor provider overriding system
29 lines
814 B
Python
29 lines
814 B
Python
"""Declarative IoC container overriding example."""
|
|
|
|
import dependency_injector.containers as containers
|
|
import dependency_injector.providers as providers
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
"""IoC container."""
|
|
|
|
sequence_factory = providers.Factory(list)
|
|
|
|
|
|
class OverridingContainer(containers.DeclarativeContainer):
|
|
"""Overriding IoC container."""
|
|
|
|
sequence_factory = providers.Factory(tuple)
|
|
|
|
|
|
# Overriding `Container` with `OverridingContainer`:
|
|
Container.override(OverridingContainer)
|
|
|
|
# Creating some objects using overridden container:
|
|
sequence_1 = Container.sequence_factory([1, 2, 3])
|
|
sequence_2 = Container.sequence_factory([3, 2, 1])
|
|
|
|
# Making some asserts:
|
|
assert Container.overridden == (OverridingContainer,)
|
|
assert sequence_1 == (1, 2, 3) and sequence_2 == (3, 2, 1)
|