2021-03-03 13:28:10 +00:00
|
|
|
"""`Singleton` provider resetting context manager example."""
|
|
|
|
|
|
|
|
from dependency_injector import containers, providers
|
|
|
|
|
|
|
|
|
|
|
|
class UserService:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
user_service = providers.Singleton(UserService)
|
|
|
|
|
|
|
|
|
2021-09-30 19:32:21 +00:00
|
|
|
if __name__ == "__main__":
|
2021-03-03 13:28:10 +00:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
user_service1 = container.user_service()
|
|
|
|
|
|
|
|
with container.user_service.reset():
|
|
|
|
user_service2 = container.user_service()
|
|
|
|
|
|
|
|
user_service3 = container.user_service()
|
|
|
|
|
|
|
|
assert user_service1 is not user_service2
|
|
|
|
assert user_service2 is not user_service3
|
|
|
|
assert user_service3 is not user_service1
|