Inject, Factory and Injects¶
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
56 57 58 59 60 61 |
|
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)
The provider can be passed in separate ways. Via direct callable or via string value where it will be automatically imported by the application.
PARAMETER | DESCRIPTION |
---|---|
provides
|
TYPE:
|
*args
|
TYPE:
|
Source code in esmerald/injector.py
14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
set_args
¶
set_args(*args)
PARAMETER | DESCRIPTION |
---|---|
*args
|
TYPE:
|
Source code in esmerald/injector.py
28 29 |
|