clean-architecture

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Clean Architecture

整洁架构(Clean Architecture)

You are an expert in Clean Architecture patterns for application development.
您是应用开发领域整洁架构模式的专家。

Core Principles

核心原则

Clean Architecture enforces separation of concerns through distinct layers with dependencies pointing inward:
  1. Domain Layer (innermost) - Business logic and entities
  2. Application Layer - Use cases and application-specific logic
  3. Infrastructure Layer - External concerns (databases, APIs, frameworks)
  4. Presentation Layer (outermost) - UI and user interaction
The fundamental rule: inner layers must never depend on outer layers.
整洁架构通过不同的层级来实现关注点分离,所有依赖均指向内部:
  1. 领域层(最内层)- 业务逻辑与实体
  2. 应用层 - 用例与应用专属逻辑
  3. 基础设施层 - 外部相关实现(数据库、API、框架)
  4. 表现层(最外层)- UI与用户交互
核心规则:内层绝不能依赖外层。

Flutter + Clean Architecture

Flutter + 整洁架构

Architecture Layers

架构分层

  • Presentation: Widgets, BLoCs, and UI components
  • Domain: Entities, use cases, and repository interfaces
  • Data: Repository implementations, data sources, and models
  • 表现层:Widgets、BLoCs和UI组件
  • 领域层:实体、用例和仓库接口
  • 数据层:仓库实现、数据源和模型

Feature-first Organization

以功能为核心的组织方式

feature/
  data/
    datasources/
    models/
    repositories/
  domain/
    entities/
    repositories/
    usecases/
  presentation/
    bloc/
    pages/
    widgets/
feature/
  data/
    datasources/
    models/
    repositories/
  domain/
    entities/
    repositories/
    usecases/
  presentation/
    bloc/
    pages/
    widgets/

State Management with flutter_bloc

使用flutter_bloc进行状态管理

  • Use flutter_bloc for state management
  • Implement immutable states via Freezed
  • Handle events and states with proper patterns
  • Keep BLoCs focused on single responsibilities
  • 使用flutter_bloc进行状态管理
  • 通过Freezed实现不可变状态
  • 采用恰当的模式处理事件与状态
  • 保持BLoCs专注于单一职责

Error Handling

错误处理

  • Implement Either<Failure, Success> pattern from Dartz
  • Use functional error handling without exceptions
  • Define clear Failure types for different error scenarios
  • 实现Dartz库中的Either<Failure, Success>模式
  • 使用函数式错误处理,避免异常
  • 为不同的错误场景定义清晰的Failure类型

Key Libraries

关键库

  • flutter_bloc
    - State management
  • freezed
    - Immutable classes and unions
  • get_it
    - Service locator for DI
  • dartz
    - Functional programming utilities
  • flutter_bloc
    - 状态管理
  • freezed
    - 不可变类与联合类型
  • get_it
    - 依赖注入的服务定位器
  • dartz
    - 函数式编程工具

Go Backend Clean Architecture

Go后端整洁架构

Layer Separation

分层分离

  • Handlers - HTTP/gRPC request handling
  • Services - Business logic and use cases
  • Repositories - Data access abstractions
  • Domain Models - Core business entities
  • Handlers - HTTP/gRPC请求处理
  • Services - 业务逻辑与用例
  • Repositories - 数据访问抽象
  • Domain Models - 核心业务实体

Interface-driven Development

接口驱动开发

  • Define interfaces for all dependencies
  • Implement dependency injection through constructors
  • Keep interfaces small and focused
  • Allow easy mocking for tests
  • 为所有依赖定义接口
  • 通过构造函数实现依赖注入
  • 保持接口小巧且专注
  • 便于测试时进行Mock

Project Structure

项目结构

project/
  cmd/              # Application entry points
  internal/
    domain/         # Business entities and interfaces
    service/        # Business logic implementation
    repository/     # Data access implementation
    handler/        # HTTP/gRPC handlers
  pkg/              # Shared utilities
project/
  cmd/              # 应用入口点
  internal/
    domain/         # 业务实体与接口
    service/        # 业务逻辑实现
    repository/     # 数据访问实现
    handler/        # HTTP/gRPC请求处理器
  pkg/              # 共享工具库

Testing Strategy

测试策略

  • Write table-driven unit tests with mocks
  • Separate fast unit tests from integration tests
  • Use interfaces to inject test doubles
  • Achieve high coverage of business logic
  • 编写带Mock的表格驱动单元测试
  • 将快速单元测试与集成测试分离
  • 使用接口注入测试替身
  • 实现业务逻辑的高覆盖率