2015-06-10 12:00:43 +03:00
|
|
|
Singleton providers
|
|
|
|
-------------------
|
|
|
|
|
2015-11-30 00:30:48 +03:00
|
|
|
.. currentmodule:: dependency_injector.providers
|
2015-11-23 15:43:11 +03:00
|
|
|
|
|
|
|
:py:class:`Singleton` provider creates new instance of specified class on
|
|
|
|
first call and returns same instance on every next call.
|
2015-06-10 12:00:43 +03:00
|
|
|
|
2015-06-24 12:29:58 +03:00
|
|
|
Example:
|
|
|
|
|
2015-07-25 00:51:14 +03:00
|
|
|
.. image:: /images/providers/singleton.png
|
2015-06-26 10:21:23 +03:00
|
|
|
:width: 80%
|
|
|
|
:align: center
|
2015-06-24 12:29:58 +03:00
|
|
|
|
2015-08-03 15:56:40 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/singleton.py
|
|
|
|
:language: python
|
2015-06-10 12:00:43 +03:00
|
|
|
|
|
|
|
Singleton providers and injections
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-12-13 15:22:59 +03:00
|
|
|
:py:class:`Singleton` provider extends :py:class:`Factory` provider, so, all
|
|
|
|
of the rules about injections are the same, as for :py:class:`Factory`
|
|
|
|
provider.
|
2015-06-24 12:29:58 +03:00
|
|
|
|
2015-06-10 12:00:43 +03:00
|
|
|
.. note::
|
|
|
|
|
2015-11-23 15:43:11 +03:00
|
|
|
Due that :py:class:`Singleton` provider creates specified class instance
|
|
|
|
only on 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 injections are done, :py:class:`Singleton` provider just
|
|
|
|
returns memorized earlier instance.
|
2015-06-10 12:00:43 +03:00
|
|
|
|
|
|
|
This may cause some problems, for example, in case of trying to bind
|
2015-11-23 15:43:11 +03:00
|
|
|
:py:class:`Factory` provider with :py:class:`Singleton` provider (provided
|
|
|
|
by dependent :py:class:`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 12:00:43 +03:00
|
|
|
|
2015-11-23 15:43:11 +03:00
|
|
|
By the way, in such case, :py:class:`Delegate` provider can be useful. It
|
|
|
|
makes possible to inject providers *as is*. Please check out full example
|
|
|
|
in *Providers delegation* section.
|
2015-06-10 12:00:43 +03:00
|
|
|
|
|
|
|
Singleton providers resetting
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-11-23 15:43:11 +03:00
|
|
|
Created and memorized by :py:class:`Singleton` instance can be reset. Reset of
|
|
|
|
:py:class:`Singleton`'s memorized instance is done by clearing reference to
|
|
|
|
it. Further lifecycle of memorized instance is out of :py:class:`Singleton`
|
|
|
|
provider's control.
|
2015-06-10 12:00:43 +03:00
|
|
|
|
2015-06-18 16:35:00 +03:00
|
|
|
Example:
|
|
|
|
|
2015-08-03 15:56:40 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/singleton_reseting.py
|
|
|
|
:language: python
|
2015-07-20 19:31:31 +03:00
|
|
|
|
|
|
|
Singleton providers delegation
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-11-23 15:43:11 +03:00
|
|
|
:py:class:`Singleton` provider could be delegated to any other provider via
|
|
|
|
any kind of injection. Delegation of :py:class:`Singleton` providers is the
|
|
|
|
same as :py:class:`Factory` providers delegation, please follow *Factory
|
|
|
|
providers delegation* section for example.
|
2015-07-20 19:31:31 +03:00
|
|
|
|
2015-11-23 15:43:11 +03:00
|
|
|
:py:class:`Singleton` delegate could be created obviously using
|
|
|
|
``Delegate(Singleton(...))`` or by calling ``Singleton(...).delegate()``
|
2015-09-02 19:06:09 +03:00
|
|
|
method.
|
2015-07-20 19:31:31 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-08-03 15:56:40 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/singleton_delegation.py
|
|
|
|
:language: python
|
2015-12-13 15:22:59 +03:00
|
|
|
|
|
|
|
Singleton providers specialization
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
:py:class:`Singleton` provider could be specialized for any kind of needs via
|
|
|
|
declaring its subclasses.
|
|
|
|
|
2015-12-15 17:48:13 +03:00
|
|
|
One of such `builtin` features is a limitation to :py:class:`Singleton`
|
|
|
|
provided type:
|
2015-12-13 15:22:59 +03:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/singleton_provided_type.py
|
|
|
|
:language: python
|