@@ -20,13 +20,12 @@ returns it on the rest of the calls.
20
20
:language: python
21
21
:lines: 3-
22
22
23
- ``Singleton `` provider handles an injection of the dependencies the same way like a
24
- :ref: `factory-provider `.
23
+ ``Singleton `` provider handles dependencies injection the same way like a :ref: `factory-provider `.
25
24
26
25
.. note ::
27
26
28
- ``Singleton `` provider does dependencies injection only when creates the object. When the object
29
- is created and memorized ``Singleton `` provider just returns it without applying the injections.
27
+ ``Singleton `` provider makes dependencies injection only when creates an object. When an object
28
+ is created and memorized ``Singleton `` provider just returns it without applying injections.
30
29
31
30
Specialization of the provided type and abstract singletons work the same like like for the
32
31
factories:
@@ -56,6 +55,21 @@ provider.
56
55
Resetting of the memorized object clears the reference to it. Further object's lifecycle is
57
56
managed by the garbage collector.
58
57
58
+ You can use ``.reset() `` method with a context manager. Memorized instance will be reset on
59
+ both entering and exiting a context.
60
+
61
+ .. literalinclude :: ../../examples/providers/singleton_resetting_with.py
62
+ :language: python
63
+ :lines: 3-
64
+ :emphasize-lines: 18-19
65
+
66
+ Context manager ``.reset() `` returns resetting singleton provider. You can use it for aliasing.
67
+
68
+ .. code-block :: python
69
+
70
+ with container.user_service.reset() as user_service:
71
+ ...
72
+
59
73
Method ``.reset() `` resets only current provider. To reset all dependent singleton providers
60
74
call ``.full_reset() `` method.
61
75
@@ -64,6 +78,13 @@ call ``.full_reset()`` method.
64
78
:lines: 3-
65
79
:emphasize-lines: 25
66
80
81
+ Method ``.full_reset() `` supports context manager interface like ``.reset() `` does.
82
+
83
+ .. code-block :: python
84
+
85
+ with container.user_service.full_reset() as user_service:
86
+ ...
87
+
67
88
See also: :ref: `reset-container-singletons `.
68
89
69
90
Using singleton with multiple threads
0 commit comments