2020-08-16 08:10:08 +03:00
|
|
|
"""Container overriding example."""
|
|
|
|
|
|
|
|
from dependency_injector import containers, providers
|
|
|
|
|
|
|
|
|
|
|
|
class Service:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceStub:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
service = providers.Factory(Service)
|
|
|
|
|
|
|
|
|
|
|
|
class OverridingContainer(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
service = providers.Factory(ServiceStub)
|
|
|
|
|
|
|
|
|
2021-09-30 22:16:17 +03:00
|
|
|
if __name__ == "__main__":
|
2020-08-16 08:10:08 +03:00
|
|
|
container = Container()
|
|
|
|
overriding_container = OverridingContainer()
|
|
|
|
|
|
|
|
container.override(overriding_container)
|
|
|
|
|
|
|
|
service = container.service()
|
|
|
|
assert isinstance(service, ServiceStub)
|