2017-12-21 23:54:44 +03:00
|
|
|
Dependency providers
|
|
|
|
--------------------
|
2015-06-18 16:34:26 +03:00
|
|
|
|
2015-11-30 00:30:48 +03:00
|
|
|
.. currentmodule:: dependency_injector.providers
|
2015-11-23 17:48:21 +03:00
|
|
|
|
2017-12-21 23:54:44 +03:00
|
|
|
:py:class:`Dependency` provider can be useful for development of
|
2017-01-30 00:12:57 +03:00
|
|
|
self-sufficient libraries / modules / applications that have required external
|
2015-06-18 16:34:26 +03:00
|
|
|
dependencies.
|
|
|
|
|
|
|
|
For example, you have created self-sufficient library / module / application,
|
|
|
|
that has dependency on *database connection*.
|
|
|
|
|
|
|
|
Second step you want to do is to make this software component to be easy
|
|
|
|
reusable by wide amount of developers and to be easily integrated into many
|
|
|
|
applications.
|
|
|
|
|
|
|
|
It may be good idea, to move all external dependencies (like
|
|
|
|
*database connection*) to the top level and make them to be injected on your
|
|
|
|
software component's initialization. It will make third party developers feel
|
2019-01-04 16:11:55 +03:00
|
|
|
themselves free about integration of your component in their applications,
|
2015-06-18 16:34:26 +03:00
|
|
|
because they would be able to find right place / right way for doing this
|
|
|
|
in their application's architectures.
|
|
|
|
|
|
|
|
At the same time, you can be sure, that your external dependency will be
|
2015-07-17 10:01:27 +03:00
|
|
|
satisfied with appropriate instance.
|
2015-06-18 16:34:26 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2016-06-09 17:37:05 +03:00
|
|
|
Class ``UsersService`` is a part of some library. ``UsersService`` has
|
2015-06-18 16:34:26 +03:00
|
|
|
dependency on database connection, which can be satisfied with any
|
|
|
|
DBAPI 2.0 database connection. Being a self-sufficient library,
|
2016-06-09 17:37:05 +03:00
|
|
|
``UsersService`` doesn't hardcode any kind of database management logic.
|
|
|
|
Instead of this, ``UsersService`` has external dependency, that has to
|
2018-01-26 20:15:07 +03:00
|
|
|
be satisfied by client's code, out of library's scope.
|
2015-06-18 16:34:26 +03:00
|
|
|
|
2017-12-21 23:54:44 +03:00
|
|
|
.. image:: /images/providers/dependency.png
|
2015-06-18 16:34:26 +03:00
|
|
|
|
2017-12-21 23:54:44 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/dependency.py
|
2015-08-03 15:45:58 +03:00
|
|
|
:language: python
|
2017-02-28 23:07:12 +03:00
|
|
|
|
|
|
|
.. disqus::
|