百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

py-clean-arch

> 后端框架
开源

一种基于 Uncle Bob 书中的 Clean Architecture 实现

197 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一种基于 Uncle Bob 书中的 Clean Architecture 实现

py-clean-arch

A Python architecture sample that demonstrates Uncle Bob's Clean Architecture through a Pokémon and Trainer API built with FastAPI. The focus is on showing how layers are separated, how dependencies flow inward, and how infrastructure can be swapped at the structural level.

This is a learning-oriented project, not a production template. For background and common questions, see the FAQ.

Architecture

This project applies Clean Architecture with the following practices:

  1. Framework Independence: FastAPI is confined to the controllers layer — use cases and models have no framework dependency.
  2. Testability: Business rules in use cases can be tested without HTTP or database.
  3. UI Independence: REST and GraphQL are interchangeable outer-layer interfaces.
  4. Database Independence: Multiple databases implement the same repository contracts.
  5. External Independence: Domain logic does not depend on specific APIs, ORMs, or drivers.

*source: yoan-thirion.gitbook.io

Project Structure

…

What This Sample Demonstrates

REST and GraphQL share the same use cases layer — the API protocol is an outer-layer detail. 5 databases (SQLite, MySQL, PostgreSQL, MongoDB, Redis) sit behind the same repository contracts — storage is also an outer-layer detail.

Two entities illustrate different complexity levels:

  • Pokemon — basic CRUD
  • Trainer — business rules like team size limits, ownership checks, and atomic trades

Enabled by several key design decisions:

  • Repository Pattern ^1 — decouples the model layer from data storage
  • Unit of Work Pattern ^2 — ensures transactional consistency
  • Dependency Injection ^3 — reduces coupling between modules
  • Asynchronous SQLAlchemy 2.0 ^4 — async database operations

Flow Diagrams

The flow diagrams visualize the layers and how they interact:

For a detailed explanation of the ASCII flow, refer to ascii-flow.md.

*source: yoan-thirion.gitbook.io

*source: https://stackoverflow.com/a/73788685

Getting Started

⚡︎ Quick Start

Start the application with a single command — no local Python or dependency installation required:

bash
$ docker compose up app

Uses in-memory SQLite by default. Access the API documentation at http://localhost:8000/docs and try the interactive endpoints.

Database Options

Set DATABASE_URI to switch databases:

Database URI format
SQLite (file) sqlite+aiosqlite:///.db
SQLite (memory) sqlite+aiosqlite:///:memory: (default)
MySQL mysql+asyncmy://:@:/
PostgreSQL postgresql+asyncpg://:@:/
MongoDB mongodb://:@:/
Redis redis://:@:/

Note: If you encounter database initialization issues, append reinitialize=true to the DATABASE_URI, e.g., sqlite+aiosqlite:///sqlite.db?reinitialize=true.

bash
$ docker compose down --remove-orphans -v
$ docker compose up dockerize

Development Setup

For local development with hot-reload:

  1. Install prerequisites: Python 3.11+ and uv

  2. Install dependencies: [^5]

    bash
    $ uv sync
  3. Launch the application:

    bash
    # With default SQLite database
    $ make up
    
    # With specific database
    $ DATABASE_URI= make up
  4. Access the application: http://localhost:8000

Testing

bash
# Test against a specific database
$ DATABASE_URI= uv run pytest

# Test against all databases (requires bats)
$ make test

For supported database URIs, see Database Options

Note: Use a different dbname with "_test" suffix for testing (e.g., "mydatabase_test") to avoid interfering with your main application data.

Changelog

See the full version history in CHANGELOG.md.

Enjoying the Project .ᐣ

If this project helped you, a ⭐ would be greatly appreciated!

Have questions or ideas? Feel free to open an issue or submit a PR.

[^5]: The uv sync command installs all required packages for running and developing the application. However, it does not include cspell. If you need cspell for spell checking, please refer to the official installation guide at cspell installation guide

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythonasyncioclean-architecturedependency-injectionfastapi

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架