Engineering
fastapi-clean-architecture avatar

fastapi-clean-architecture

Build modular FastAPI applications using Clean Architecture, including domain-driven design, dependency injection, repository patterns, and testing strategies for scalable Python backend services.

Introduction

This skill provides a structured framework for developing maintainable, testable, and scalable Python backend services using the FastAPI framework. It enforces a strict separation of concerns through the Three-Layer Architecture: the API Layer for HTTP routing and DTO validation, the Domain Layer for pure business logic and entity definitions, and the Infrastructure Layer for external integration and data persistence. By implementing Dependency Injection and the Repository Pattern, the skill ensures that business logic remains independent of specific frameworks, databases, or external APIs like PostgreSQL and Redis.

  • Implements clean layer separation: API (controllers/schemas), Domain (entities/services), and Infrastructure (repositories/adapters).

  • Enforces strict unidirectional dependency flow: API depends on Domain; Infrastructure implements Domain interfaces; Domain remains framework-agnostic.

  • Provides templates for domain-driven design (DDD), including value objects, abstract repository contracts, and custom domain exceptions.

  • Integrates robust testing strategies covering integration, unit, and mock-based testing for async Python environments.

  • Facilitates the use of Pydantic for request/response DTOs and SQLAlchemy for database ORM operations.

  • Best used when initiating new enterprise-grade FastAPI projects or refactoring monolithic legacy code into maintainable segments.

  • Requires familiarity with Python type hinting, asynchronous programming (async/await), and dependency injection principles.

  • Inputs typically include project requirements, data models, and business rules, while outputs comprise organized, production-ready directory structures and modularized codebase components.

  • Ideal for developers building systems that require high testability and the ability to swap infrastructure components without modifying core business logic.

  • Constraint: Follows strict domain independence—the domain layer must not import from API or Infrastructure to maintain architectural integrity.

Repository Stats

Stars
1
Forks
0
Open Issues
0
Language
Python
Default Branch
main
Sync Status
Idle
Last Synced
May 3, 2026, 05:44 PM
View on GitHub