mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-30 19:24:31 +03:00
Update writing of custom providers docs
This commit is contained in:
parent
9b4e325a2d
commit
1fd54927c5
Binary file not shown.
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 36 KiB |
|
@ -1,27 +1,28 @@
|
|||
Writing of custom providers
|
||||
------------------------
|
||||
|
||||
.. module:: dependency_injector.providers
|
||||
|
||||
List of *Dependency Injector* providers could be widened with custom providers.
|
||||
|
||||
Below are some tips and recommendations that have to be met:
|
||||
|
||||
1. Every custom provider has to extend base provider class -
|
||||
``di.Provider``.
|
||||
2. Cusom provider's ``__init__()`` could be overriden with only condition:
|
||||
parent initializer (``di.Provider.__init__()``) has to be called.
|
||||
:py:class:`Provider`.
|
||||
2. Cusom provider's ``__init__()`` could be overriden, but parent's
|
||||
initializer (:py:meth:`Provider.__init__`) has to be called.
|
||||
3. Providing strategy has to be implemented in custom provider's
|
||||
``_provide()`` method. All ``*args`` & ``**kwargs`` that will be
|
||||
recieved by ``di.Provider.__call__()`` will be transefed to custom
|
||||
provider's ``_provide()``.
|
||||
:py:meth:`Provider._provide` method. All ``*args`` & ``**kwargs``
|
||||
that will be recieved by :py:meth:`Provider.__call__` will be
|
||||
transefed to custom provider's :py:meth:`Provider._provide`.
|
||||
4. If custom provider is based on some standard providers, it is better to
|
||||
use delegation of standard providers, then extending of them.
|
||||
5. If custom provider defines any attributes, it is good to list them in
|
||||
``__slots__`` attribute (as *Dependency Injector* does). It can save
|
||||
some memory.
|
||||
6. If custom provider deals with injections (e.g. ``di.Factory``,
|
||||
``di.Singleton`` providers), it is strongly recommended to be
|
||||
consistent with ``di.Factory``, ``di.Singleton`` and ``di.Callable``
|
||||
providers style.
|
||||
6. If custom provider deals with injections, it is strongly recommended
|
||||
to be consistent with :py:class:`Factory`, :py:class:`Singleton` and
|
||||
:py:class:`Callable` providers style.
|
||||
|
||||
Example:
|
||||
|
||||
|
|
|
@ -1,24 +1,33 @@
|
|||
"""Custom `di.Factory` example."""
|
||||
"""Custom `Factory` example."""
|
||||
|
||||
import dependency_injector as di
|
||||
from dependency_injector import providers
|
||||
|
||||
|
||||
class User(object):
|
||||
"""Example class User."""
|
||||
|
||||
|
||||
class UsersFactory(di.Provider):
|
||||
class UsersFactory(providers.Provider):
|
||||
"""Example users factory."""
|
||||
|
||||
__slots__ = ('_factory',)
|
||||
|
||||
def __init__(self):
|
||||
"""Initializer."""
|
||||
self._factory = di.Factory(User)
|
||||
self._factory = providers.Factory(User)
|
||||
super(UsersFactory, self).__init__()
|
||||
|
||||
def _provide(self, *args, **kwargs):
|
||||
"""Return provided instance."""
|
||||
"""Return provided instance.
|
||||
|
||||
:param args: tuple of context positional arguments
|
||||
:type args: tuple[object]
|
||||
|
||||
:param kwargs: dictionary of context keyword arguments
|
||||
:type kwargs: dict[str, object]
|
||||
|
||||
:rtype: object
|
||||
"""
|
||||
return self._factory(*args, **kwargs)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user