diff --git a/docs/images/providers/custom_provider.png b/docs/images/providers/custom_provider.png index 784c90bd..398e6bfa 100644 Binary files a/docs/images/providers/custom_provider.png and b/docs/images/providers/custom_provider.png differ diff --git a/docs/providers/custom.rst b/docs/providers/custom.rst index 1b3d0fd9..effd87fa 100644 --- a/docs/providers/custom.rst +++ b/docs/providers/custom.rst @@ -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: diff --git a/examples/providers/custom_factory.py b/examples/providers/custom_factory.py index 2264c736..7193fbaa 100644 --- a/examples/providers/custom_factory.py +++ b/examples/providers/custom_factory.py @@ -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)