mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-07 07:00:49 +03:00
Update @di.inject decorator docs for 0.9.x release
This commit is contained in:
parent
57d5a30726
commit
62df0bb853
|
@ -8,22 +8,21 @@ Current section of documentation describes advanced usage of
|
||||||
@inject decorator
|
@inject decorator
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
``@inject`` decorator can be used for making *inline* dependency injections.
|
``@di.inject`` decorator can be used for making *inline* dependency injections.
|
||||||
It *patches* decorated callable in such way that dependency injection will be
|
It *patches* decorated callable in such way that dependency injection will be
|
||||||
done during every call of decorated callable.
|
done during every call of decorated callable.
|
||||||
|
|
||||||
``@inject`` decorator takes only argument that is supposed to be an
|
``@di.inject`` decorator takes keyword argument, that will be injected during
|
||||||
``dependency_injector.injections.KwArg`` injection.
|
every next call of decorated callback with the same name. Any Python object
|
||||||
|
will be injected *as is*, except ``di.Provider``'s, which will be called to
|
||||||
Any Python object will be injected *as is*, except *Dependency Injector*
|
provide injectable values.
|
||||||
providers, that will be called to provide injectable value.
|
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/advanced_usage/inject_decorator_simple.py
|
.. literalinclude:: ../../examples/advanced_usage/inject_decorator_simple.py
|
||||||
:language: python
|
:language: python
|
||||||
|
|
||||||
Example of dependecy injection in Flask view:
|
Example of usage ``@di.inject`` decorator with Flask:
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/advanced_usage/inject_decorator_flask.py
|
.. literalinclude:: ../../examples/advanced_usage/inject_decorator_flask.py
|
||||||
:language: python
|
:language: python
|
||||||
|
|
|
@ -1,30 +1,24 @@
|
||||||
"""`@inject` decorator and Flask view example."""
|
"""`@di.inject` decorator and Flask view example."""
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import flask
|
||||||
from flask import Flask
|
import dependency_injector as di
|
||||||
|
|
||||||
from dependency_injector.providers import Singleton
|
|
||||||
from dependency_injector.injections import KwArg
|
|
||||||
from dependency_injector.injections import Attribute
|
|
||||||
from dependency_injector.injections import inject
|
|
||||||
|
|
||||||
|
|
||||||
# Database and `ObjectA` providers.
|
# Database and `ObjectA` providers.
|
||||||
database = Singleton(sqlite3.Connection,
|
database = di.Singleton(sqlite3.Connection,
|
||||||
KwArg('database', ':memory:'),
|
database=':memory:',
|
||||||
KwArg('timeout', 30),
|
timeout=30,
|
||||||
KwArg('detect_types', True),
|
detect_types=True,
|
||||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
isolation_level='EXCLUSIVE')
|
||||||
Attribute('row_factory', sqlite3.Row))
|
|
||||||
|
|
||||||
# Flask application:
|
# Flask application:
|
||||||
app = Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
# Flask view with @inject decorator:
|
# Flask view with @inject decorator:
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
@inject(KwArg('database', database))
|
@di.inject(database=database)
|
||||||
def hello(database):
|
def hello(database):
|
||||||
"""Example Flask view."""
|
"""Example Flask view."""
|
||||||
one = database.execute('SELECT 1').fetchone()[0]
|
one = database.execute('SELECT 1').fetchone()[0]
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
"""`@inject` decorator simple example."""
|
"""`@di.inject` decorator simple example."""
|
||||||
|
|
||||||
from dependency_injector.providers import Factory
|
import dependency_injector as di
|
||||||
from dependency_injector.injections import KwArg
|
|
||||||
from dependency_injector.injections import inject
|
|
||||||
|
|
||||||
|
|
||||||
dependency_injector_factory = Factory(object)
|
dependency_injector_factory = di.Factory(object)
|
||||||
|
|
||||||
|
|
||||||
@inject(KwArg('new_object', dependency_injector_factory))
|
@di.inject(new_object=dependency_injector_factory)
|
||||||
@inject(KwArg('some_setting', 1334))
|
@di.inject(some_setting=1334)
|
||||||
def example_callback(new_object, some_setting):
|
def example_callback(new_object, some_setting):
|
||||||
"""Example callback that does some asserts with input args."""
|
"""Example callback that does some asserts with input args."""
|
||||||
assert isinstance(new_object, object)
|
assert isinstance(new_object, object)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user