Update docs about writing custom providers

This commit is contained in:
Roman Mogilatov 2015-09-02 17:20:19 +03:00
parent ef5c360a30
commit 54da9913eb
3 changed files with 13 additions and 18 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 28 KiB

View File

@ -6,26 +6,22 @@ List of *Dependency Injector* providers could be widened with custom providers.
Below are some tips and recommendations that have to be met: Below are some tips and recommendations that have to be met:
1. Every custom provider has to extend base provider class - 1. Every custom provider has to extend base provider class -
``dependency_injector.providers.Provider``. ``di.Provider``.
2. Cusom provider's ``__init__()`` could be overriden with only condition: 2. Cusom provider's ``__init__()`` could be overriden with only condition:
parent initializer parent initializer (``di.Provider.__init__()``) has to be called.
(``dependency_injector.providers.Provider.__init__()``) has to be called.
3. Providing strategy has to be implemented in custom provider's 3. Providing strategy has to be implemented in custom provider's
``_provide()`` method. All ``*args`` & ``**kwargs`` that will be ``_provide()`` method. All ``*args`` & ``**kwargs`` that will be
recieved by ``dependency_injector.providers.Provider.__call__()`` will be recieved by ``di.Provider.__call__()`` will be transefed to custom
transefed provider's ``_provide()``.
to custom provider's ``_provide()``.
4. If custom provider is based on some standard providers, it is better to 4. If custom provider is based on some standard providers, it is better to
use delegation of standard providers, then extending of them. use delegation of standard providers, then extending of them.
5. If custom provider defines any attributes, it is good to list them in 5. If custom provider defines any attributes, it is good to list them in
``__slots__`` attribute (as *Dependency Injector* does). It can save ``__slots__`` attribute (as *Dependency Injector* does). It can save
some memory. some memory.
6. If custom provider deals with injections (e.g. ``Factory``, 6. If custom provider deals with injections (e.g. ``di.Factory``,
``Singleton`` providers), it is strongly recommended to use ``di.Singleton`` providers), it is strongly recommended to be
``dependency_injector.injections.Injection`` and its subclasses: consistent with ``di.Factory``, ``di.Singleton`` and ``di.Callable``
``dependency_injector.injections.KwArg``, providers style.
``dependency_injector.injections.Attribute`` and
``dependency_injector.injections.Method``.
Example: Example:

View File

@ -1,7 +1,6 @@
"""Custom `Factory` example.""" """Custom `di.Factory` example."""
from dependency_injector.providers import Provider import dependency_injector as di
from dependency_injector.providers import Factory
class User(object): class User(object):
@ -9,7 +8,7 @@ class User(object):
"""Example class User.""" """Example class User."""
class UsersFactory(Provider): class UsersFactory(di.Provider):
"""Example users factory.""" """Example users factory."""
@ -17,7 +16,7 @@ class UsersFactory(Provider):
def __init__(self): def __init__(self):
"""Initializer.""" """Initializer."""
self._factory = Factory(User) self._factory = di.Factory(User)
super(UsersFactory, self).__init__() super(UsersFactory, self).__init__()
def _provide(self, *args, **kwargs): def _provide(self, *args, **kwargs):