CL

Clean Architecture

Used in 498 python projects (avg ★81)

Details

Language / Topic
pythonPython
Category
Architecture

Rules

balanced
- Separate code into layers: `domain/` (entities, business rules), `application/` (use cases), `infrastructure/` (DB, APIs, frameworks).
- Domain layer has zero external dependencies — no Django, no SQLAlchemy, no FastAPI imports.
- Use Python protocols (`typing.Protocol`) for ports — adapters implement the protocol without explicit inheritance.
- Define repository interfaces (protocols) in the domain layer. Implement with SQLAlchemy/Django ORM in infrastructure.
- Use cases orchestrate domain logic: accept input DTOs, call domain services, return output DTOs.
- Use dependency injection (manual or `dependency-injector`) to wire infrastructure to domain interfaces.
- Controllers (FastAPI routes, Django views) live in infrastructure and call application use cases.