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, DiderectInjects
ravyn.Inject
¶
Inject(dependency, use_cache=False, **kwargs)
Bases: ArbitraryHashableBaseModel
Source code in ravyn/injector.py
67 68 69 70 71 72 |
|
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/params.py
636 637 638 639 640 641 642 643 644 645 646 647 648 |
|
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
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.
Source code in ravyn/injector.py
36 37 38 39 |
|