2015-06-10 09:00:43 +00:00
|
|
|
Singleton providers
|
|
|
|
-------------------
|
|
|
|
|
2015-09-02 16:06:09 +00:00
|
|
|
``di.Singleton`` provider creates new instance of specified class on first call
|
2015-06-10 09:00:43 +00:00
|
|
|
and returns same instance on every next call.
|
|
|
|
|
2015-06-24 09:29:58 +00:00
|
|
|
Example:
|
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/singleton.png
|
2015-06-26 07:21:23 +00:00
|
|
|
:width: 80%
|
|
|
|
:align: center
|
2015-06-24 09:29:58 +00:00
|
|
|
|
2015-08-03 12:56:40 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/singleton.py
|
|
|
|
:language: python
|
2015-06-10 09:00:43 +00:00
|
|
|
|
|
|
|
Singleton providers and injections
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-02 16:06:09 +00:00
|
|
|
``di.Singleton`` providers use ``di.Factory`` providers for first creation of
|
2015-06-10 09:00:43 +00:00
|
|
|
specified class instance, so, all of the rules about injections are the same,
|
2015-09-02 16:06:09 +00:00
|
|
|
as for ``di.Factory`` providers.
|
2015-06-10 09:00:43 +00:00
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/singleton_internals.png
|
2015-06-26 07:21:23 +00:00
|
|
|
:width: 80%
|
|
|
|
:align: center
|
2015-06-24 09:29:58 +00:00
|
|
|
|
2015-06-10 09:00:43 +00:00
|
|
|
.. note::
|
|
|
|
|
2015-09-02 16:06:09 +00:00
|
|
|
Due that ``di.Singleton`` provider creates specified class instance only on
|
2015-06-10 09:00:43 +00:00
|
|
|
the first call, all injections are done once, during the first call, also.
|
|
|
|
Every next call, while instance has been already created and memorized, no
|
2015-09-02 16:06:09 +00:00
|
|
|
injections are done, ``di.Singleton`` provider just returns memorized
|
|
|
|
earlier instance.
|
2015-06-10 09:00:43 +00:00
|
|
|
|
|
|
|
This may cause some problems, for example, in case of trying to bind
|
2015-09-02 16:06:09 +00:00
|
|
|
``di.Factory`` provider with ``di.Singleton`` provider (provided by
|
|
|
|
dependent ``di.Factory`` instance will be injected only once, during the
|
|
|
|
first call). Be aware that such behaviour was made with opened eyes and is
|
|
|
|
not a bug.
|
2015-06-10 09:00:43 +00:00
|
|
|
|
2015-09-02 16:06:09 +00:00
|
|
|
By the way, in such case, ``di.Delegate`` provider can be useful. It makes
|
2015-06-10 09:00:43 +00:00
|
|
|
possible to inject providers *as is*. Please check out full example in
|
|
|
|
*Providers delegation* section.
|
|
|
|
|
|
|
|
Singleton providers resetting
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-02 16:06:09 +00:00
|
|
|
Created and memorized by ``di.Singleton`` instance can be reset. Reset of
|
|
|
|
``di.Singleton``'s memorized instance is done by clearing reference to it.
|
|
|
|
Further lifecycle of memorized instance is out of ``di.Singleton`` provider's
|
|
|
|
control.
|
2015-06-10 09:00:43 +00:00
|
|
|
|
2015-06-18 13:35:00 +00:00
|
|
|
Example:
|
|
|
|
|
2015-08-03 12:56:40 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/singleton_reseting.py
|
|
|
|
:language: python
|
2015-07-20 16:31:31 +00:00
|
|
|
|
|
|
|
Singleton providers delegation
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-02 16:06:09 +00:00
|
|
|
``di.Singleton`` provider could be delegated to any other provider via any
|
|
|
|
kind of injection. Delegation of ``di.Singleton`` providers is the same as
|
|
|
|
``di.Factory`` providers delegation, please follow
|
|
|
|
*Factory providers delegation* section for example.
|
2015-07-20 16:31:31 +00:00
|
|
|
|
2015-09-02 16:06:09 +00:00
|
|
|
``di.Singleton`` delegate could be created obviously using
|
|
|
|
``di.Delegate(di.Singleton())`` or by calling ``di.Singleton.delegate()``
|
|
|
|
method.
|
2015-07-20 16:31:31 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-08-03 12:56:40 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/singleton_delegation.py
|
|
|
|
:language: python
|