2015-09-02 16:06:09 +00:00
|
|
|
"""`di.Singleton` providers example."""
|
2015-06-10 09:00:43 +00:00
|
|
|
|
2015-09-02 16:06:09 +00:00
|
|
|
import dependency_injector as di
|
2015-06-10 09:00:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserService(object):
|
|
|
|
"""Example class UserService."""
|
|
|
|
|
|
|
|
# Singleton provider creates new instance of specified class on first call and
|
|
|
|
# returns same instance on every next call.
|
2015-09-02 16:06:09 +00:00
|
|
|
users_service_provider = di.Singleton(UserService)
|
2015-06-10 09:00:43 +00:00
|
|
|
|
|
|
|
# Retrieving several UserService objects:
|
|
|
|
user_service1 = users_service_provider()
|
|
|
|
user_service2 = users_service_provider()
|
|
|
|
|
|
|
|
# Making some asserts:
|
|
|
|
assert user_service1 is user_service2
|
|
|
|
assert isinstance(user_service1, UserService)
|
|
|
|
assert isinstance(user_service2, UserService)
|