2015-11-23 12:43:11 +00:00
|
|
|
"""`Singleton` providers example."""
|
2015-06-10 09:00:43 +00:00
|
|
|
|
2016-06-08 14:46:40 +00:00
|
|
|
import collections
|
|
|
|
|
2016-06-06 08:54:05 +00:00
|
|
|
import dependency_injector.providers as providers
|
2015-06-10 09:00:43 +00:00
|
|
|
|
|
|
|
|
2016-06-08 14:46:40 +00:00
|
|
|
UsersService = collections.namedtuple('UsersService', [])
|
2015-06-10 09:00:43 +00:00
|
|
|
|
|
|
|
# Singleton provider creates new instance of specified class on first call and
|
|
|
|
# returns same instance on every next call.
|
2016-06-08 14:46:40 +00:00
|
|
|
users_service_provider = providers.Singleton(UsersService)
|
2015-06-10 09:00:43 +00:00
|
|
|
|
|
|
|
# Retrieving several UserService objects:
|
2016-06-08 14:46:40 +00:00
|
|
|
users_service1 = users_service_provider()
|
|
|
|
users_service2 = users_service_provider()
|
2015-06-10 09:00:43 +00:00
|
|
|
|
|
|
|
# Making some asserts:
|
2016-06-08 14:46:40 +00:00
|
|
|
assert users_service1 is users_service2
|