2015-08-05 17:22:25 +03:00
|
|
|
Advanced usage
|
|
|
|
==============
|
|
|
|
|
2015-08-31 16:31:38 +03:00
|
|
|
Current section of documentation describes advanced usage of
|
|
|
|
*Dependency Injector*.
|
2015-08-05 17:22:25 +03:00
|
|
|
|
|
|
|
@inject decorator
|
|
|
|
-----------------
|
|
|
|
|
2015-11-30 00:30:48 +03:00
|
|
|
.. currentmodule:: dependency_injector.injections
|
2015-11-23 20:26:39 +03:00
|
|
|
|
2016-09-23 00:24:45 +03:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
:py:func:`inject` decorator has been deprecated since version 2.2.0.
|
|
|
|
|
2015-11-23 20:26:39 +03:00
|
|
|
:py:func:`inject` decorator is a part of
|
|
|
|
:py:mod:`dependency_injector.injections` module.
|
|
|
|
|
|
|
|
:py:func:`inject` decorator can be used for making *inline* dependency
|
2015-09-02 12:23:01 +03:00
|
|
|
injections. It *patches* decorated callable in such way that dependency
|
|
|
|
injection will be done during every call of decorated callable.
|
2015-08-05 17:22:25 +03:00
|
|
|
|
2015-11-23 20:26:39 +03:00
|
|
|
:py:func:`inject` takes a various number of positional and keyword arguments
|
2015-10-23 15:04:04 +03:00
|
|
|
that are used as decorated callable injections. Every time, when
|
2015-11-23 20:26:39 +03:00
|
|
|
:py:func:`inject` is called, positional and keyword argument injections would
|
|
|
|
be passed as an callable arguments.
|
2015-10-23 15:04:04 +03:00
|
|
|
|
|
|
|
Such behaviour is very similar to the standard Python ``functools.partial``
|
|
|
|
object, except of one thing: all injectable values are provided
|
2015-11-23 20:26:39 +03:00
|
|
|
*"as is"*, except of providers (subclasses of
|
|
|
|
:py:class:`dependency_injector.providers.Provider`). Providers
|
2015-10-23 15:04:04 +03:00
|
|
|
will be called every time, when injection needs to be done. For example,
|
2015-11-23 20:26:39 +03:00
|
|
|
if injectable value of injection is a
|
|
|
|
:py:class:`dependency_injector.providers.Factory`, it will provide new one
|
2015-10-23 15:04:04 +03:00
|
|
|
instance (as a result of its call) every time, when injection needs to be done.
|
|
|
|
|
2015-11-23 20:26:39 +03:00
|
|
|
:py:func:`inject` behaviour with context positional and keyword arguments is
|
2015-10-23 15:04:04 +03:00
|
|
|
very like a standard Python ``functools.partial``:
|
|
|
|
|
2015-11-23 20:26:39 +03:00
|
|
|
- Positional context arguments will be appended after :py:func:`inject`
|
2015-10-23 15:04:04 +03:00
|
|
|
positional injections.
|
2015-11-23 20:26:39 +03:00
|
|
|
- Keyword context arguments have priority on :py:func:`inject` keyword
|
2015-10-23 15:04:04 +03:00
|
|
|
injections and will be merged over them.
|
2015-08-05 17:22:25 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-09-28 21:56:36 +03:00
|
|
|
.. literalinclude:: ../../examples/advanced_usage/inject_simple.py
|
2015-08-05 17:22:25 +03:00
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|
2015-08-05 17:22:25 +03:00
|
|
|
|
2015-11-23 20:26:39 +03:00
|
|
|
Example of usage :py:func:`inject` decorator with Flask:
|
2015-08-05 17:22:25 +03:00
|
|
|
|
2015-09-28 21:56:36 +03:00
|
|
|
.. literalinclude:: ../../examples/advanced_usage/inject_flask.py
|
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|
2015-09-28 21:56:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
@inject decorator with classes
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-11-23 20:26:39 +03:00
|
|
|
:py:func:`inject` could be applied for classes. In such case, it will look for
|
2015-09-28 22:10:17 +03:00
|
|
|
class ``__init__()`` method and pass injection to it. If decorated class has
|
2015-11-23 20:26:39 +03:00
|
|
|
no ``__init__()`` method, appropriate
|
|
|
|
:py:exc:`dependency_injector.errors.Error` will be raised.
|
2015-09-28 21:56:36 +03:00
|
|
|
|
2015-11-23 20:26:39 +03:00
|
|
|
Example of usage :py:func:`inject` with Flask class-based view:
|
2015-09-28 21:56:36 +03:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/advanced_usage/inject_flask_class_based.py
|
2015-08-05 17:22:25 +03:00
|
|
|
:language: python
|
2016-04-11 10:43:02 +03:00
|
|
|
:linenos:
|