017. Dependency injection framework: dependency-injector
- Date: 2026-06-15
- Status: Accepted
Decision
Section titled “Decision”In the context ofa growing FastAPI + Lambda ingestion app where infrastructure objects (BedrockEmbedder,BedrockLLM, DB connection factory) are constructed at call sites inpipeline.pyandapp.py,facingfragile test setup (module-levelunittest.mock.patchintercepts that break on refactor) and compounding wiring complexity as routes and Lambda handlers multiply,we decided fordependency-injectorwith aDeclarativeContainerinrag_sample/container.py— each infrastructure object declared as aproviders.Singletonorproviders.Factory; FastAPI routes and ingestion functions receive dependencies from the container rather than constructing them directly; tests usecontainer.override()to inject stubs with nopatchneeded,and neglected- Manual kwarg defaults (
def embed(embedder=None): if embedder is None: embedder = BedrockEmbedder()) — zero new dependencies, but per-function optional-parameter wiring scales poorly and test setup stays verbose lagom— auto-wires by type annotation with less boilerplate; rejected for low adoption (289K vs 5.25M monthly downloads as of 2026-06-15) and fewer production references- FastAPI
Depends()— fits per-request HTTP dependencies but does not address ingestion pipeline wiring or Lambda handler setup outside the request lifecycle
- Manual kwarg defaults (
to achieveexplicit, auditable wiring declared once in the container, with zeropatchcalls in tests and a single-point swap for provider substitution (e.g. local embedder for dev),acceptinga new framework dependency that is Python-only and carries no AWS or FastAPI opinions.
Consequences
Section titled “Consequences”rag_sample/container.pyowns all infrastructure wiring; consumers import the container, not concrete classes directly.- Adding a new provider (e.g. OpenAI embedder for dev) requires one new provider declaration in the container and a config toggle — no changes to call sites.
- The framework is Python-only and has no AWS or FastAPI opinions, so it does not constrain future infrastructure choices.
© 2026 Benjamin Arunski