mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-31 03:36:41 +03:00
Adding explanation for Singleton and Callable providers delegation
This commit is contained in:
parent
996fc4ecbf
commit
0efebb337f
|
@ -5,6 +5,9 @@ Callable providers
|
|||
some injections. Every call of this provider returns result of call of initial
|
||||
callable.
|
||||
|
||||
Callable providers and injections
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
``Callable`` provider uses ``KwArg`` injections. ``KwArg`` injections are
|
||||
done by passing injectable values as keyword arguments during call time.
|
||||
|
||||
|
@ -36,3 +39,35 @@ Example:
|
|||
# Making some asserts (client's code):
|
||||
hashed_password = password_hasher('super secret')
|
||||
assert password_verifier('super secret', hashed_password)
|
||||
|
||||
Callable providers delegation
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
``Callable`` provider could be delegated to any other provider via any kind of
|
||||
injection. Delegation of ``Callable`` providers is the same as ``Factory`` and
|
||||
``Singleton`` providers delegation, please follow *Factory providers
|
||||
delegation* section for example.
|
||||
|
||||
``Callable`` delegate could be created obviously using
|
||||
``Delegate(Callable())`` or by calling ``Callable.delegate()`` method.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
"""`Callable` providers delegation example."""
|
||||
|
||||
import sys
|
||||
|
||||
from objects.providers import Callable
|
||||
from objects.providers import Delegate
|
||||
|
||||
|
||||
# Some callable provider and few delegates of it:
|
||||
callable_provider = Callable(sys.exit)
|
||||
callable_provider_delegate1 = callable_provider.delegate()
|
||||
callable_provider_delegate2 = Delegate(callable_provider)
|
||||
|
||||
# Making some asserts:
|
||||
assert callable_provider_delegate1() is callable_provider
|
||||
assert callable_provider_delegate2() is callable_provider
|
||||
|
|
|
@ -102,3 +102,33 @@ Example:
|
|||
|
||||
# Making some asserts:
|
||||
assert user_service3 is not user_service1
|
||||
|
||||
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.
|
||||
|
||||
``Singleton`` delegate could be created obviously using
|
||||
``Delegate(Singleton())`` or by calling ``Singleton.delegate()`` method.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
"""`Singleton` providers delegation example."""
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import Delegate
|
||||
|
||||
|
||||
# Some singleton provider and few delegates of it:
|
||||
singleton_provider = Singleton(object)
|
||||
singleton_provider_delegate1 = singleton_provider.delegate()
|
||||
singleton_provider_delegate2 = Delegate(singleton_provider)
|
||||
|
||||
# Making some asserts:
|
||||
assert singleton_provider_delegate1() is singleton_provider
|
||||
assert singleton_provider_delegate2() is singleton_provider
|
||||
|
|
16
examples/providers/callable_delegation.py
Normal file
16
examples/providers/callable_delegation.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
"""`Callable` providers delegation example."""
|
||||
|
||||
import sys
|
||||
|
||||
from objects.providers import Callable
|
||||
from objects.providers import Delegate
|
||||
|
||||
|
||||
# Some callable provider and few delegates of it:
|
||||
callable_provider = Callable(sys.exit)
|
||||
callable_provider_delegate1 = callable_provider.delegate()
|
||||
callable_provider_delegate2 = Delegate(callable_provider)
|
||||
|
||||
# Making some asserts:
|
||||
assert callable_provider_delegate1() is callable_provider
|
||||
assert callable_provider_delegate2() is callable_provider
|
14
examples/providers/singleton_delegation.py
Normal file
14
examples/providers/singleton_delegation.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
"""`Singleton` providers delegation example."""
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import Delegate
|
||||
|
||||
|
||||
# Some singleton provider and few delegates of it:
|
||||
singleton_provider = Singleton(object)
|
||||
singleton_provider_delegate1 = singleton_provider.delegate()
|
||||
singleton_provider_delegate2 = Delegate(singleton_provider)
|
||||
|
||||
# Making some asserts:
|
||||
assert singleton_provider_delegate1() is singleton_provider
|
||||
assert singleton_provider_delegate2() is singleton_provider
|
Loading…
Reference in New Issue
Block a user