2015-06-18 13:34:26 +00:00
|
|
|
External dependency providers
|
|
|
|
-----------------------------
|
|
|
|
|
2015-11-23 14:48:21 +00:00
|
|
|
.. module:: dependency_injector.providers
|
|
|
|
|
|
|
|
:py:class:`ExternalDependency` provider can be useful for development of
|
2015-06-18 13:34:26 +00:00
|
|
|
self-sufficient libraries / modules / applications that has required external
|
|
|
|
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
|
|
|
|
themselves free about integration of yours component in their applications,
|
|
|
|
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 07:01:27 +00:00
|
|
|
satisfied with appropriate instance.
|
2015-06-18 13:34:26 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Class ``UserService`` is a part of some library. ``UserService`` has
|
|
|
|
dependency on database connection, which can be satisfied with any
|
|
|
|
DBAPI 2.0 database connection. Being a self-sufficient library,
|
|
|
|
``UserService`` doesn't hardcode any kind of database management logic.
|
2015-07-17 07:01:27 +00:00
|
|
|
Instead of this, ``UserService`` has external dependency, that has to
|
|
|
|
be satisfied by cleint's code, out of library's scope.
|
2015-06-18 13:34:26 +00:00
|
|
|
|
2015-07-24 21:51:14 +00:00
|
|
|
.. image:: /images/providers/external_dependency.png
|
2015-06-18 13:34:26 +00:00
|
|
|
|
2015-08-03 12:45:58 +00:00
|
|
|
.. literalinclude:: ../../examples/providers/external_dependency.py
|
|
|
|
:language: python
|