Request scope

A “request scope” is the most common unit of work for DI in web apps.

The pattern

  1. Pick a scope name (commonly Scope.REQUEST).

  2. Register request-local services with lifetime=Lifetime.SCOPED and scope=Scope.REQUEST.

  3. Enter the scope for each request and resolve your handler/service graph inside it.

Example (runnable)

See Scopes & cleanup and FastAPI for runnable request-scope examples.

Notes

  • Scoped services are reused within a single request, but not across requests.

  • Use generator/async-generator factories for cleanup (DB sessions, connections). See Resources (cleanup).