2020-09-01 23:04:48 +03:00
|
|
|
"""`Singleton` provider example."""
|
2015-06-10 12:00:43 +03:00
|
|
|
|
2020-09-03 23:56:01 +03:00
|
|
|
from dependency_injector import containers, providers
|
2016-06-08 17:46:40 +03:00
|
|
|
|
2015-06-10 12:00:43 +03:00
|
|
|
|
2020-09-01 23:04:48 +03:00
|
|
|
class UserService:
|
|
|
|
...
|
2015-06-10 12:00:43 +03:00
|
|
|
|
|
|
|
|
2020-09-03 23:56:01 +03:00
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
user_service_provider = providers.Singleton(UserService)
|
2015-06-10 12:00:43 +03:00
|
|
|
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2020-09-03 23:56:01 +03:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
user_service1 = container.user_service_provider()
|
|
|
|
user_service2 = container.user_service_provider()
|
2020-09-01 23:04:48 +03:00
|
|
|
assert user_service1 is user_service2
|