fastware v0.1.0 /src.fastware.di
On this page

Dependency injection container providing per-request resolution with automatic caching, generator cleanup, and scope-aware dependency override support.

#src.fastware.di

#src.fastware.di

Dependency injection container providing per-request resolution with automatic caching, generator cleanup, and scope-aware dependency override support.

Supports sync/async factory callables and sync/async generator factories (yield pattern). Generator factories get cleanup after the handler returns. Results are cached per-request: the same factory called twice returns the same resolved instance.

#DependencyResolver

Resolves a dict of {name: factory} into {name: value} per request.

overrides maps an original factory to a replacement factory. When an override exists for a factory, the replacement is called instead.

#resolve

python
async def resolve(self, deps: dict[str, Callable], request: Any) -> tuple[dict[str, Any], list[tuple[str, Any]]]

Resolve deps against request.

Returns (resolved, cleanups) where resolved is a {name: value} dict and cleanups is a list of ("sync" | "async", generator) pairs to pass to :meth:cleanup.

If a factory raises during resolution, any generator cleanups accumulated so far are run before the exception propagates.

#cleanup

python
async def cleanup(cleanups: list[tuple[str, Any]]) -> None

Run generator cleanups in reverse order.

Cleanup errors are suppressed -- a failing cleanup must not mask the handler's response or exception.