Inject, Factory and Injects¶
Warning
The current page still doesn't have a translation for this language.
But you can help translating it: Contributing.
Esmerald dependency injection system is actually pretty simple and can be checked in the official dependency injection section for more details.
from esmerald import Inject, Injects, Factory, DiderectInjects
esmerald.Inject
¶
Inject(dependency, use_cache=False, **kwargs)
Bases: ArbitraryHashableBaseModel
PARAMETER | DESCRIPTION |
---|---|
dependency
|
TYPE:
|
use_cache
|
TYPE:
|
**kwargs
|
TYPE:
|
Source code in esmerald/injector.py
67 68 69 70 71 72 |
|
esmerald.Injects
¶
Injects(default=Undefined, skip_validation=False, allow_none=True)
Bases: FieldInfo
Creates a FieldInfo class with extra parameters. This is used for dependencies and to inject them.
Example
@get(dependencies={"value": Inject(lambda: 13)})
def myview(value: Injects()):
return {"value": value}
PARAMETER | DESCRIPTION |
---|---|
default
|
TYPE:
|
skip_validation
|
TYPE:
|
allow_none
|
TYPE:
|
Source code in esmerald/params.py
622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
extra
instance-attribute
¶
extra = {IS_DEPENDENCY: True, SKIP_VALIDATION: skip_validation, 'allow_none': allow_none}
esmerald.Factory
¶
Factory(provides, *args, **kwargs)
A dependency injection factory that supports both positional and keyword arguments.
The provider can be passed as either: - A direct callable - A string reference to be dynamically imported
PARAMETER | DESCRIPTION |
---|---|
provides
|
TYPE:
|
*args
|
TYPE:
|
**kwargs
|
TYPE:
|
Example Usage
dependencies = { "user": Factory(UserDAO, db_session=session, cache=cache) }
Source code in esmerald/injector.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
set_args
¶
set_args(*args, **kwargs)
Set or update arguments dynamically.
PARAMETER | DESCRIPTION |
---|---|
*args
|
TYPE:
|
**kwargs
|
TYPE:
|
Source code in esmerald/injector.py
36 37 38 39 |
|