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
617 618 619 620 621 622 623 624 625 626 627 628 629 |
|
extra
instance-attribute
¶
extra = {IS_DEPENDENCY: True, SKIP_VALIDATION: skip_validation, 'allow_none': allow_none}
esmerald.DirectInjects
¶
DirectInjects(dependency=None, *, use_cache=True, allow_none=True)
This function should be only called if Inject/Injects is not used in the dependencies. This is a simple wrapper of the classic Inject().
PARAMETER | DESCRIPTION |
---|---|
dependency
|
TYPE:
|
use_cache
|
TYPE:
|
allow_none
|
TYPE:
|
Source code in esmerald/param_functions.py
6 7 8 9 10 11 12 13 14 15 16 |
|
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 |
|