mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Update Singleton provider docs
This commit is contained in:
parent
1e1266a4d3
commit
98c4ec71c1
Binary file not shown.
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 10 KiB |
Binary file not shown.
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 10 KiB |
|
@ -1,7 +1,7 @@
|
|||
Singleton providers
|
||||
-------------------
|
||||
|
||||
``Singleton`` provider creates new instance of specified class on first call
|
||||
``di.Singleton`` provider creates new instance of specified class on first call
|
||||
and returns same instance on every next call.
|
||||
|
||||
Example:
|
||||
|
@ -16,9 +16,9 @@ Example:
|
|||
Singleton providers and injections
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
``Singleton`` providers use ``Factory`` providers for first creation of
|
||||
``di.Singleton`` providers use ``di.Factory`` providers for first creation of
|
||||
specified class instance, so, all of the rules about injections are the same,
|
||||
as for ``Factory`` providers.
|
||||
as for ``di.Factory`` providers.
|
||||
|
||||
.. image:: /images/providers/singleton_internals.png
|
||||
:width: 80%
|
||||
|
@ -26,27 +26,29 @@ as for ``Factory`` providers.
|
|||
|
||||
.. note::
|
||||
|
||||
Due that ``Singleton`` provider creates specified class instance only on
|
||||
Due that ``di.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, ``Singleton`` provider just returns memorized earlier
|
||||
instance.
|
||||
injections are done, ``di.Singleton`` provider just returns memorized
|
||||
earlier instance.
|
||||
|
||||
This may cause some problems, for example, in case of trying to bind
|
||||
``Factory`` provider with ``Singleton`` provider (provided by dependent
|
||||
``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.
|
||||
``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.
|
||||
|
||||
By the way, in such case, ``Delegate`` provider can be useful. It makes
|
||||
By the way, in such case, ``di.Delegate`` provider can be useful. It makes
|
||||
possible to inject providers *as is*. Please check out full example in
|
||||
*Providers delegation* section.
|
||||
|
||||
Singleton providers resetting
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Created and memorized by ``Singleton`` instance can be reset. Reset of
|
||||
``Singleton``'s memorized instance is done by clearing reference to it. Further
|
||||
lifecycle of memorized instance is out of ``Singleton`` provider's control.
|
||||
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.
|
||||
|
||||
Example:
|
||||
|
||||
|
@ -56,13 +58,14 @@ Example:
|
|||
Singleton providers delegation
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
``Singleton`` provider could be delegated to any other provider via any kind of
|
||||
injection. Delegation of ``Singleton`` providers is the same as ``Factory``
|
||||
providers delegation, please follow *Factory providers delegation* section for
|
||||
example.
|
||||
``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.
|
||||
|
||||
``Singleton`` delegate could be created obviously using
|
||||
``Delegate(Singleton())`` or by calling ``Singleton.delegate()`` method.
|
||||
``di.Singleton`` delegate could be created obviously using
|
||||
``di.Delegate(di.Singleton())`` or by calling ``di.Singleton.delegate()``
|
||||
method.
|
||||
|
||||
Example:
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Singleton` providers example."""
|
||||
"""`di.Singleton` providers example."""
|
||||
|
||||
from dependency_injector.providers import Singleton
|
||||
import dependency_injector as di
|
||||
|
||||
|
||||
class UserService(object):
|
||||
|
@ -9,7 +9,7 @@ class UserService(object):
|
|||
|
||||
# Singleton provider creates new instance of specified class on first call and
|
||||
# returns same instance on every next call.
|
||||
users_service_provider = Singleton(UserService)
|
||||
users_service_provider = di.Singleton(UserService)
|
||||
|
||||
# Retrieving several UserService objects:
|
||||
user_service1 = users_service_provider()
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
"""`Singleton` providers delegation example."""
|
||||
"""`di.Singleton` providers delegation example."""
|
||||
|
||||
from dependency_injector.providers import Singleton
|
||||
from dependency_injector.providers import Delegate
|
||||
import dependency_injector as di
|
||||
|
||||
|
||||
# Some singleton provider and few delegates of it:
|
||||
singleton_provider = Singleton(object)
|
||||
singleton_provider = di.Singleton(object)
|
||||
singleton_provider_delegate1 = singleton_provider.delegate()
|
||||
singleton_provider_delegate2 = Delegate(singleton_provider)
|
||||
singleton_provider_delegate2 = di.Delegate(singleton_provider)
|
||||
|
||||
# Making some asserts:
|
||||
assert singleton_provider_delegate1() is singleton_provider
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Singleton` providers resetting example."""
|
||||
"""`di.Singleton` providers resetting example."""
|
||||
|
||||
from dependency_injector.providers import Singleton
|
||||
import dependency_injector as di
|
||||
|
||||
|
||||
class UserService(object):
|
||||
|
@ -8,7 +8,7 @@ class UserService(object):
|
|||
"""Example class UserService."""
|
||||
|
||||
# Users service singleton provider:
|
||||
users_service_provider = Singleton(UserService)
|
||||
users_service_provider = di.Singleton(UserService)
|
||||
|
||||
# Retrieving several UserService objects:
|
||||
user_service1 = users_service_provider()
|
||||
|
|
Loading…
Reference in New Issue
Block a user