mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
Improve singleton provider docs
This commit is contained in:
parent
ad260fe709
commit
441cc66427
|
@ -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
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
|
|
24
examples/providers/singleton_multiple_containers.py
Normal file
24
examples/providers/singleton_multiple_containers.py
Normal 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
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user