Inject, Factory and Injects¶
Ravyn dependency injection system is actually pretty simple and can be checked in the official dependency injection section for more details.
from ravyn import Inject, Injects, Factory
ravyn.Inject
¶
Inject(dependency, use_cache=False, **kwargs)
Bases: ArbitraryHashableBaseModel
Ravyn's dependency injector built on Lilya's dependency system internally. Keeps full backward compatibility with the old behavior and interface.
Source code in ravyn/injector.py
79 80 81 82 83 84 85 |
|
ravyn.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}
Source code in ravyn/injector.py
137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
ravyn.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
Example Usage
dependencies = { "user": Factory(UserDAO, db_session=session, cache=cache) }
Source code in ravyn/injector.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
set_args
¶
set_args(*args, **kwargs)
Set or update arguments dynamically.
Source code in ravyn/injector.py
43 44 45 46 |
|