Adding new integrationsΒΆ
Though there are some integrations in library you are not limited to use them.
The main points are:
Find a way to pass a global container instance. Often it is attached to application instance or passed by a middleware.
Find a place to enter and exit request scope and how to pass the container to a handler. Usually, it is entered in a middleware and container is stored in some kind of request context.
Alternatively, you can use the
wrap_injectionfunction withmanage_scope=Trueto automate entering and exiting the request scope without relying on middleware. When enabled,manage_scopeensures that the container passed towrap_injectionenters and exits the next scope. Custom Scope TargetingFor more complex cases, you can pass a
scopeargument towrap_injectionto specify a custom scope to enter on function call. For example, you can create decorators that target specific scopes in the dependency hierarchy:@inject(scope=Scope.STEP) async def handler(step_dep: StepDep = FromDishka[StepDep]): ... @inject(scope=Scope.ACTION) def process_action(action_dep: ActionDep = FromDishka[ActionDep]): ...
When you use
manage_scope=Trueor specify a customscope, you can passprovide_contextfunction that allows to populate the container context with passed arguments. This is useful when you want to pass the function scoped arguments to other dependencies without relying on middleware.Configure a decorator. The main option here is to provide a way for retrieving container. Often, need to modify handler signature adding additional parameters. It is also available.
Check if you can apply decorator automatically.
While writing middlewares and working with scopes is done by your custom code, we have a helper for creating @inject decorators - a wrap_injection function.
container_getteris a function with two params(args, kwargs)which is called to get a container used to retrieve dependencies within scope.additional_paramsis a list ofinspect.Parameterwhich should be added to handler signature.
For more details, check existing integrations.