Improve singleton provider docs

This commit is contained in:
Roman Mogylatov 2020-09-03 17:57:26 -04:00
parent ad260fe709
commit 441cc66427
3 changed files with 33 additions and 2 deletions

View File

@ -32,6 +32,13 @@ factories:
- :ref:`factory-specialize-provided-type` - :ref:`factory-specialize-provided-type`
- :ref:`abstract-factory` - :ref:`abstract-factory`
``Singleton`` provider scope is tied to the container. Two different containers will provider
two different singleton objects:
.. literalinclude:: ../../examples/providers/singleton_multiple_containers.py
:language: python
:lines: 3-
Resetting memorized object Resetting memorized object
-------------------------- --------------------------

View File

@ -0,0 +1,24 @@
"""`Singleton` provider resetting example."""
from dependency_injector import containers, providers
class UserService:
...
class Container(containers.DeclarativeContainer):
user_service_provider = providers.Singleton(UserService)
if __name__ == '__main__':
container1 = Container()
user_service1 = container1.user_service_provider()
assert user_service1 is container1.user_service_provider()
container2 = Container()
user_service2 = container2.user_service_provider()
assert user_service2 is container2.user_service_provider()
assert user_service1 is not user_service2

View File

@ -19,5 +19,5 @@ if __name__ == '__main__':
container.user_service_provider.reset() container.user_service_provider.reset()
users_service2 = container.user_service_provider() user_service2 = container.user_service_provider()
assert users_service2 is not user_service1 assert user_service2 is not user_service1