Skip to content

Latest commit

 

History

History
36 lines (30 loc) · 2.29 KB

CONTEXT.md

File metadata and controls

36 lines (30 loc) · 2.29 KB

modern-di-fastapi

The modern-di integration for FastAPI: it opens the container across the app's lifespan, opens a scoped child container per connection, and resolves declared dependencies into route and WebSocket parameters through FastAPI's own Depends.

Language

A term is listed only when there is a synonym to reject, or a meaning subtle enough that code and docs must agree on it. General programming vocabulary does not belong here, however heavily this package uses it.

The domain terms are modern-di's: Container (root and child), Provider, Group, Scope, Resolution, Override, Connection. That project's CONTEXT.md is the authority for all of them; nothing here redefines one. FastAPI's are FastAPI's: dependency, Depends, router, request, WebSocket. The two below are this package's own.

Per-connection container: The child container build_di_container opens for one connection and closes when it ends: REQUEST-scoped for a fastapi.Request, SESSION-scoped for a fastapi.WebSocket. Every FromDI parameter in one handler resolves from the same one. Avoid: per-request container, request container. A WebSocket's is SESSION-scoped, so "request" names one of the two kinds while reading as the wrong scope for the other.

Composed lifespan: The app's own lifespan nested inside the container's open/close, installed by setup_di onto app.router.lifespan_context. The container opens before the original's startup code and closes after its shutdown code, so that code can resolve APP-scoped dependencies and the APP finalizers run last. The original's yielded state passes through untouched. It also opens and closes the root containers of mounted FastAPI apps that called setup_di, since Starlette never runs a mounted app's lifespan. An app whose setup_di got manage_lifespan=False keeps its root out of the composed lifespan, whether it is the outer app or a mounted one; the user opens and closes that root. Avoid: merged lifespan. "Merge" is FastAPI's own word for the private helper this package deliberately does not use, which also merges the two yielded states (ADR-0001). Also avoid appended lifespan: the app's lifespan runs inside the container's open/close.