Writing of custom providers
.. currentmodule:: 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:
- Every custom provider has to extend base provider class - :py:class:`Provider`.
- Cusom provider's
__init__()could be overriden, but parent's initializer (:py:meth:`Provider.__init__`) has to be called.
- Providing strategy has to be implemented in custom provider's :py:meth:`Provider.__call__` method.
- If custom provider is based on some standard providers, it is better to use delegation of standard providers, then extending of them.
- If custom provider defines any attributes, it is good to list them in
__slots__attribute (as Dependency Injector does). It can save some memory.
- 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.
.. literalinclude:: ../../examples/providers/custom_factory.py :language: python :linenos: