mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 16:07:51 +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 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 | ||||
| done during every call of decorated callable. | ||||
| 
 | ||||
| ``@inject`` decorator takes only argument that is supposed to be an | ||||
| ``dependency_injector.injections.KwArg`` injection. | ||||
| 
 | ||||
| Any Python object will be injected *as is*, except *Dependency Injector*  | ||||
| providers, that will be called to provide injectable value. | ||||
| ``@di.inject`` decorator takes keyword argument, that will be injected during  | ||||
| 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  | ||||
| provide injectable values. | ||||
| 
 | ||||
| Example: | ||||
| 
 | ||||
| .. literalinclude:: ../../examples/advanced_usage/inject_decorator_simple.py | ||||
|    :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 | ||||
|    :language: python | ||||
|  |  | |||
|  | @ -1,30 +1,24 @@ | |||
| """`@inject` decorator and Flask view example.""" | ||||
| """`@di.inject` decorator and Flask view example.""" | ||||
| 
 | ||||
| import sqlite3 | ||||
| 
 | ||||
| from flask import Flask | ||||
| 
 | ||||
| from dependency_injector.providers import Singleton | ||||
| from dependency_injector.injections import KwArg | ||||
| from dependency_injector.injections import Attribute | ||||
| from dependency_injector.injections import inject | ||||
| import flask | ||||
| import dependency_injector as di | ||||
| 
 | ||||
| 
 | ||||
| # Database and `ObjectA` providers. | ||||
| database = Singleton(sqlite3.Connection, | ||||
|                      KwArg('database', ':memory:'), | ||||
|                      KwArg('timeout', 30), | ||||
|                      KwArg('detect_types', True), | ||||
|                      KwArg('isolation_level', 'EXCLUSIVE'), | ||||
|                      Attribute('row_factory', sqlite3.Row)) | ||||
| database = di.Singleton(sqlite3.Connection, | ||||
|                         database=':memory:', | ||||
|                         timeout=30, | ||||
|                         detect_types=True, | ||||
|                         isolation_level='EXCLUSIVE') | ||||
| 
 | ||||
| # Flask application: | ||||
| app = Flask(__name__) | ||||
| app = flask.Flask(__name__) | ||||
| 
 | ||||
| 
 | ||||
| # Flask view with @inject decorator: | ||||
| @app.route('/') | ||||
| @inject(KwArg('database', database)) | ||||
| @di.inject(database=database) | ||||
| def hello(database): | ||||
|     """Example Flask view.""" | ||||
|     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 | ||||
| from dependency_injector.injections import KwArg | ||||
| from dependency_injector.injections import inject | ||||
| import dependency_injector as di | ||||
| 
 | ||||
| 
 | ||||
| dependency_injector_factory = Factory(object) | ||||
| dependency_injector_factory = di.Factory(object) | ||||
| 
 | ||||
| 
 | ||||
| @inject(KwArg('new_object', dependency_injector_factory)) | ||||
| @inject(KwArg('some_setting', 1334)) | ||||
| @di.inject(new_object=dependency_injector_factory) | ||||
| @di.inject(some_setting=1334) | ||||
| def example_callback(new_object, some_setting): | ||||
|     """Example callback that does some asserts with input args.""" | ||||
|     assert isinstance(new_object, object) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user