Using with frameworks¶
There are some integrations in library, however you are not limited to them only. You can create custom integrations for your framework of choice. Some of the integrations are supported by a community, refer to their documentation as well.
Web frameworks |
Telegram bots |
Tasks and events |
Other |
|---|---|---|---|
FastStream (com) |
Flet (com) |
||
ag2 (com) |
|||
RQ (com) |
|||
Pyramid (com) |
Jobify (com) |
||
APScheduler (com) |
|||
Starlette (com) |
|||
Strawberry (com) |
|||
Quart (com) |
If you have another framework refer Adding new integrations
See also the real integration examples here.
Common approach¶
For several frameworks library contains helper functions so you don’t need to control scopes yourself, but just annotate handler/view functions and change application startup code.
To use framework integration you mainly need to do 4 things:
call
setup_dishkaon your container and framework entityadd
FromDishka[YourClass]on you framework handlers (or view-functions)decorate your handlers with
@injectbefore registering them in framework. Some integrations do not require it, see their detailsadd additional provider to the container to access framework specific objects from your provider.
Note
FromDishka[T] is basically a synonym for Annotated[T, FromComponent()] and is used to get an object from default component. To use other component you can use the same syntax with annotated Annotated[T, FromComponent("X")].
For more details on components see Components and providers isolation
For such integrations library enters scope for each generated event. So, if you have standard scope, than handler dependencies will be retrieved as for Scope.REQUEST.
For streaming protocols and websockets you will be also to have SESSION-scoped objects with a lifespan of the whole stream.
Additionally, you may need to call container.close() in the end of your application lifecycle if you want to finalize APP-scoped dependencies.
Some frameworks have their own specific, check corresponding page.
For FastAPI it will look like:
from dishka.integrations.fastapi import FromDishka, inject, setup_dishka, FastapiProvider
@router.get("/")
@inject
async def index(interactor: FromDishka[Interactor]) -> str:
result = interactor()
return result
app = FastAPI()
container = make_async_container(your_provider, FastapiProvider())
setup_dishka(container, app)