Layering Rules
Summary
The dependency direction is fixed. Violating it is a design defect.
Allowed imports
Forbidden imports
Why
- The business logic is testable without a server or an event loop.
- Adapters can be swapped (in-memory fake, real database) without touching business logic.
- The HTTP edge stays thin and replaceable.
The port/adapter contract
The ports/ package is the seam where the domain meets infrastructure.
Each domain/<resource>/ports/ module declares the resource's models/document
shape and exposes factory functions that bind a concrete adapter — so a port
module is allowed to import from infra/ and from the persistence libraries.
Everything outside ports/ in domain/ stays framework- and driver-free.
The composition root calls the port factories; nothing else constructs an adapter.
Enforcement
domain/must never import the web framework (fastapi/starlette) orinterfaces/. Only theports/seam,infra/, and the composition root touch drivers.- Reviewers (and CI) reject
import fastapiinsidedomain/orinfra/.