Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions architecture/container-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ entry point an application calls at startup. It does three things:
1. Stores the container on `app.state.di_container` (read back by
`fetch_di_container(app)`).
2. Registers the two *context providers*
(`fastapi_request_provider`, `fastapi_websocket_provider`) on the container's
`providers_registry`, so the live `Request` / `WebSocket` can be resolved.
(`fastapi_request_provider`, `fastapi_websocket_provider`) via
`container.add_providers(...)`, so the live `Request` / `WebSocket` can be
resolved.
3. Composes the container's open/close onto the app's existing
`lifespan_context` via `_compose_lifespan`, preserving any lifespan the app
already had (its startup/shutdown still run and its yielded state passes
Expand Down
10 changes: 4 additions & 6 deletions architecture/dependency-resolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ type, so the annotated parameter type stays accurate.
`Dependency` is a frozen, slotted, generic dataclass holding the requested
`dependency`. Its `__call__` is itself a FastAPI dependency: it depends on
`build_di_container`, so it receives the *child container* for the current
connection, then resolves against it:

- **Provider** (`isinstance(..., AbstractProvider)`) →
`request_container.resolve_provider(self.dependency)`.
- **Type** (anything else) →
`request_container.resolve(dependency_type=self.dependency)`.
connection, then resolves against it via
`request_container.resolve_dependency(self.dependency)` — a provider argument
resolves by reference, a plain type resolves by type; overrides, caching, and
suggestions are inherited from whichever it dispatches to.

Because resolution flows through `build_di_container`, every `FromDI`
dependency in a request shares that request's scoped container and its
Expand Down
6 changes: 2 additions & 4 deletions modern_di_fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def composed(app_: fastapi.FastAPI) -> typing.AsyncIterator[typing.Mapping

def setup_di(app: fastapi.FastAPI, container: Container) -> Container:
app.state.di_container = container
container.providers_registry.add_providers(*_CONNECTION_PROVIDERS)
container.add_providers(*_CONNECTION_PROVIDERS)
app.router.lifespan_context = _compose_lifespan(app.router.lifespan_context)
return container

Expand All @@ -73,9 +73,7 @@ class Dependency(typing.Generic[T_co]):
async def __call__(
self, request_container: typing.Annotated[Container, fastapi.Depends(build_di_container)]
) -> T_co:
if isinstance(self.dependency, providers.AbstractProvider):
return request_container.resolve_provider(self.dependency)
return request_container.resolve(dependency_type=self.dependency)
return request_container.resolve_dependency(self.dependency)


def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [
"Typing :: Typed",
"Topic :: Software Development :: Libraries",
]
dependencies = ["fastapi>=0.100,<1", "modern-di>=2.21.0,<3"]
dependencies = ["fastapi>=0.100,<1", "modern-di>=2.25,<3"]
version = "0"

[project.urls]
Expand Down
Loading