2020-09-03 01:15:06 +00:00
|
|
|
.. _create-provider:
|
|
|
|
|
2020-09-03 01:21:07 +00:00
|
|
|
Creating a custom provider
|
|
|
|
==========================
|
2015-07-27 22:29:31 +00:00
|
|
|
|
2020-09-03 00:59:55 +00:00
|
|
|
.. meta::
|
|
|
|
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Custom provider, Create
|
|
|
|
:description: This page demonstrates how to create a custom provider.
|
2015-07-27 22:29:31 +00:00
|
|
|
|
2020-09-03 00:59:55 +00:00
|
|
|
.. currentmodule:: dependency_injector.providers
|
2015-07-27 22:29:31 +00:00
|
|
|
|
2020-09-03 00:59:55 +00:00
|
|
|
You can create a custom provider.
|
2015-07-27 22:29:31 +00:00
|
|
|
|
2020-09-03 00:59:55 +00:00
|
|
|
To create a custom provider you need to follow these rules:
|
2015-07-27 22:29:31 +00:00
|
|
|
|
2020-09-03 00:59:55 +00:00
|
|
|
1. New provider class should inherit :py:class:`Provider`.
|
|
|
|
2. You need to implement the ``Provider._provide()`` method.
|
|
|
|
3. You need to implement the ``Provider.__deepcopy__()`` method. It should return an
|
2022-12-19 02:38:12 +00:00
|
|
|
equivalent copy of a provider. All providers must be copied with the ``deepcopy()`` function
|
2022-12-19 02:42:55 +00:00
|
|
|
from the ``providers`` module. It's essential to pass ``memo`` into ``deepcopy`` in order to keep
|
|
|
|
the preconfigured ``args`` and ``kwargs`` of stored providers. After the a new provider object
|
|
|
|
is created, use ``Provider._copy_overriding()`` method to copy all overriding providers. See the
|
|
|
|
example below.
|
2021-02-05 23:27:32 +00:00
|
|
|
4. If new provider has a ``__init__()`` method, it should call the parent
|
2020-09-03 00:59:55 +00:00
|
|
|
``Provider.__init__()``.
|
2021-02-05 23:27:32 +00:00
|
|
|
5. If new provider stores any other providers, these providers should be listed in
|
|
|
|
``.related`` property. Property ``.related`` also should yield providers from parent
|
|
|
|
``.related`` property.
|
2015-07-27 22:29:31 +00:00
|
|
|
|
2015-08-03 12:47:42 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/custom_factory.py
|
|
|
|
:language: python
|
2020-09-03 00:59:55 +00:00
|
|
|
:lines: 3-
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
1. Prefer delegation over inheritance. If you choose between inheriting a ``Factory`` or
|
|
|
|
inheriting a ``Provider`` and use ``Factory`` internally - the last is better.
|
2022-12-19 02:38:12 +00:00
|
|
|
2. When creating a new provider follow the ``Factory``-like injections style. Consistency matters.
|
2020-09-03 00:59:55 +00:00
|
|
|
3. Use the ``__slots__`` attribute to make sure nothing could be attached to your provider. You
|
|
|
|
will also save some memory.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
If you don't find needed provider in the ``providers`` module and experience troubles creating
|
|
|
|
one by your own - open a
|
|
|
|
`Github Issue <https://github.com/ets-labs/python-dependency-injector/issues>`_.
|
|
|
|
|
|
|
|
I'll help you to resolve the issue if that's possible. If the new provider can be useful for
|
|
|
|
others I'll include it into the ``providers`` module.
|
2017-02-28 20:07:12 +00:00
|
|
|
|
|
|
|
.. disqus::
|