litestar-di

Configure Litestar dependency injection with Provide maps and Dishka scoped providers.

Updated Aug 17, 2026
One-click install
npx skills add https://github.com/renjianguo666/litecms --skill litestar-di-renjianguo666
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: litestar-di
Source: https://github.com/renjianguo666/litecms/tree/main/.agents/skills/litestar-di
Command: npx skills add https://github.com/renjianguo666/litecms --skill litestar-di-renjianguo666

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires litestar, dishka, sqlalchemy, and includes references (resource) components.

What problem does it solve? Wiring dependencies in a Litestar application—database sessions, services, current users—requires choosing the right scope and the right DI mechanism, and mixing conventions leads to fragile, hard-to-test code. This Skill guides consistent use of Litestar's built-in Provide() dependency maps and Dishka integration for scoped provider management. ## Core Features & Use Cases - Built-in DI with Provide(): Register app-, router-, controller-, and handler-level dependency maps with correct override semantics. - Dishka integration: Set up Provider classes, Scope.REQUEST/Scope.APP, make_async_container, and FromDishka as Inject for explicit scope management. - Decision guidance: A comparison table clarifies when to stay on Provide() versus scaling up to Dishka. - Use Case: You are building a Litestar API where each request needs an async database session and a user service. Use this Skill to register request-scoped providers, inject them into controllers, and keep tests able to override providers cleanly. ## Quick Start Ask the AI to wire a request-scoped database session and user service into your Litestar app using Provide or Dishka.

Frequently Asked Questions about litestar-di

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I inject dependencies in Litestar with Provide?

Define an async provider function and register it in the dependencies dict, for example dependencies={"db_session": Provide(provide_session)} on the Litestar app, router, controller, or route handler. Litestar resolves the value by name when a handler declares it as a parameter.

When should I use Dishka instead of Litestar Provide?

Stay on Provide() for small apps with under roughly ten dependencies and flat graphs. Move to Dishka when you need explicit cross-scope management (app singletons plus request transients), repetitive hand-wiring, or shared services across CLI and HTTP entry points.

How does Dishka integrate with Litestar?

Create Provider classes with a Scope, build a container via make_async_container, and call setup_dishka(container, app). Handlers then declare parameters annotated with FromDishka (aliased as Inject[T]) to trigger resolution per request.

How does Litestar dependency override ordering work?

Same-name lookups resolve inward: a handler-level dependency overrides the controller's, which overrides the app's. This lets tests or specific routes replace providers without changing global registration.

Why should request-scoped resources not open at import time?

Opening database sessions or clients at import time creates global mutable state that leaks across requests and breaks scope lifetimes. Providers should create and close such resources within their declared scope so tests can override them cleanly.