DDD项目目录结构示例

ecommerce-platform/
├── README.md
├── docker-compose.yml
├── requirements.txt
└── src/
    ├── shared/                           # 共享内核
    │   ├── __init__.py
    │   ├── domain/
    │   │   ├── __init__.py
    │   │   ├── base_entity.py
    │   │   ├── base_aggregate_root.py
    │   │   ├── domain_event.py
    │   │   ├── value_objects/
    │   │   │   ├── __init__.py
    │   │   │   ├── money.py
    │   │   │   ├── email.py
    │   │   │   └── phone.py
    │   │   └── exceptions/
    │   │       ├── __init__.py
    │   │       ├── domain_exception.py
    │   │       └── business_rule_violation.py
    │   ├── infrastructure/
    │   │   ├── __init__.py
    │   │   ├── database/
    │   │   │   ├── __init__.py
    │   │   │   ├── base_repository.py
    │   │   │   └── unit_of_work.py
    │   │   ├── messaging/
    │   │   │   ├── __init__.py
    │   │   │   ├── event_bus.py
    │   │   │   └── message_broker.py
    │   │   └── utils/
    │   │       ├── __init__.py
    │   │       ├── id_generator.py
    │   │       └── datetime_utils.py
    │   └── application/
    │       ├── __init__.py
    │       ├── base_command.py
    │       ├── base_query.py
    │       └── base_application_service.py
    │
    ├── core_domains/                     # 核心域
    │   ├── __init__.py
    │   │
    │   ├── catalog/                      # 核心域1:商品目录域
    │   │   ├── __init__.py
    │   │   ├── bounded_contexts/
    │   │   │   ├── __init__.py
    │   │   │   │
    │   │   │   ├── product_management/   # 限界上下文1:商品管理
    │   │   │   │   ├── __init__.py
    │   │   │   │   ├── domain/
    │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   ├── aggregates/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   ├── product/         # 聚合1:商品聚合
    │   │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   │   ├── product.py           # 聚合根
    │   │   │   │   │   │   │   ├── product_variant.py   # 实体
    │   │   │   │   │   │   │   ├── product_attribute.py # 值对象
    │   │   │   │   │   │   │   └── product_specification.py # 规约
    │   │   │   │   │   │   ├── category/        # 聚合2:分类聚合
    │   │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   │   ├── category.py          # 聚合根
    │   │   │   │   │   │   │   ├── category_hierarchy.py # 值对象
    │   │   │   │   │   │   │   └── category_rules.py    # 领域服务
    │   │   │   │   │   │   └── brand/           # 聚合3:品牌聚合
    │   │   │   │   │   │       ├── __init__.py
    │   │   │   │   │   │       ├── brand.py             # 聚合根
    │   │   │   │   │   │       ├── brand_authorization.py # 实体
    │   │   │   │   │   │       └── brand_policy.py      # 值对象
    │   │   │   │   │   ├── value_objects/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   ├── product_id.py
    │   │   │   │   │   │   ├── sku.py
    │   │   │   │   │   │   ├── price.py
    │   │   │   │   │   │   └── weight.py
    │   │   │   │   │   ├── entities/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   └── product_review.py
    │   │   │   │   │   ├── domain_services/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   ├── product_validation_service.py
    │   │   │   │   │   │   └── pricing_service.py
    │   │   │   │   │   ├── repositories/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   ├── product_repository.py
    │   │   │   │   │   │   ├── category_repository.py
    │   │   │   │   │   │   └── brand_repository.py
    │   │   │   │   │   └── events/
    │   │   │   │   │       ├── __init__.py
    │   │   │   │   │       ├── product_created.py
    │   │   │   │   │       ├── product_updated.py
    │   │   │   │   │       └── product_discontinued.py
    │   │   │   │   ├── application/
    │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   ├── commands/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   ├── create_product_command.py
    │   │   │   │   │   │   ├── update_product_command.py
    │   │   │   │   │   │   └── discontinue_product_command.py
    │   │   │   │   │   ├── queries/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   ├── get_product_query.py
    │   │   │   │   │   │   └── search_products_query.py
    │   │   │   │   │   ├── handlers/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   ├── product_command_handler.py
    │   │   │   │   │   │   └── product_query_handler.py
    │   │   │   │   │   └── services/
    │   │   │   │   │       ├── __init__.py
    │   │   │   │   │       └── product_application_service.py
    │   │   │   │   ├── infrastructure/
    │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   ├── persistence/
    │   │   │   │   │   │   ├── __init__.py
    │   │   │   │   │   │   ├── sqlalchemy_product_repository.py
    │   │   │   │   │   │   ├── sqlalchemy_category_repository.py
    │   │   │   │   │   │   └── models/
    │   │   │   │   │   │       ├── __init__.py
    │   │   │   │   │   │       ├── product_model.py
    │   │   │   │   │   │       └── category_model.py
    │   │   │   │   │   └── external_services/
    │   │   │   │   │       ├── __init__.py
    │   │   │   │   │       └── image_service.py
    │   │   │   │   └── presentation/
    │   │   │   │       ├── __init__.py
    │   │   │   │       ├── api/
    │   │   │   │       │   ├── __init__.py
    │   │   │   │       │   ├── product_controller.py
    │   │   │   │       │   └── category_controller.py
    │   │   │   │       └── dto/
    │   │   │   │           ├── __init__.py
    │   │   │   │           ├── product_dto.py
    │   │   │   │           └── category_dto.py
    │   │   │   │
    │   │   │   └── inventory_management/     # 限界上下文2:库存管理
    │   │   │       ├── __init__.py
    │   │   │       ├── domain/
    │   │   │       │   ├── __init__.py
    │   │   │       │   ├── aggregates/
    │   │   │       │   │   ├── __init__.py
    │   │   │       │   │   └── inventory/
    │   │   │       │   │       ├── __init__.py
    │   │   │       │   │       ├── inventory.py
    │   │   │       │   │       ├── stock_movement.py
    │   │   │       │   │       └── reorder_policy.py
    │   │   │       │   └── repositories/
    │   │   │       │       ├── __init__.py
    │   │   │       │       └── inventory_repository.py
    │   │   │       ├── application/
    │   │   │       └── infrastructure/
    │   │   └── shared/                   # 该域内共享
    │   │       ├── __init__.py
    │   │       └── catalog_shared_types.py
    │   │
    │   └── trading/                      # 核心域2:交易域
    │       ├── __init__.py
    │       ├── bounded_contexts/
    │       │   ├── __init__.py
    │       │   ├── order_management/     # 订单管理上下文
    │       │   │   ├── __init__.py
    │       │   │   ├── domain/
    │       │   │   │   ├── aggregates/
    │       │   │   │   │   ├── order/
    │       │   │   │   │   │   ├── order.py
    │       │   │   │   │   │   ├── order_item.py
    │       │   │   │   │   │   └── order_status.py
    │       │   │   │   │   └── shopping_cart/
    │       │   │   │   │       ├── cart.py
    │       │   │   │   │       └── cart_item.py
    │       │   │   │   └── repositories/
    │       │   │   │       └── order_repository.py
    │       │   │   ├── application/
    │       │   │   ├── infrastructure/
    │       │   │   └── presentation/
    │       │   └── payment_processing/   # 支付处理上下文
    │       │       ├── __init__.py
    │       │       ├── domain/
    │       │       ├── application/
    │       │       ├── infrastructure/
    │       │       └── presentation/
    │       └── shared/
    │           ├── __init__.py
    │           └── trading_shared_types.py
    │
    └── supporting_domains/               # 支撑域
        ├── __init__.py
        └── user_management/              # 支撑域:用户管理域
            ├── __init__.py
            ├── bounded_contexts/
            │   ├── __init__.py
            │   ├── authentication/       # 认证上下文
            │   │   ├── __init__.py
            │   │   ├── domain/
            │   │   │   ├── aggregates/
            │   │   │   │   └── user/
            │   │   │   │       ├── user.py
            │   │   │   │       ├── user_credential.py
            │   │   │   │       └── login_session.py
            │   │   │   └── repositories/
            │   │   │       └── user_repository.py
            │   │   ├── application/
            │   │   ├── infrastructure/
            │   │   └── presentation/
            │   └── profile_management/   # 用户档案管理上下文
            │       ├── __init__.py
            │       ├── domain/
            │       ├── application/
            │       ├── infrastructure/
            │       └── presentation/
            └── shared/
                ├── __init__.py
                └── user_shared_types.py

关键设计说明

1. 域的分层

  • core_domains/: 核心域,企业的核心竞争力
  • supporting_domains/: 支撑域,支持核心业务
  • shared/: 共享内核,跨域共享的基础设施

2. 限界上下文的体现

catalog 核心域中:

  • product_management: 商品管理上下文
  • inventory_management: 库存管理上下文

每个上下文都有完整的分层架构。

3. 聚合的组织

product_management 上下文的 domain/aggregates/ 中:

  • product/: 商品聚合(包含商品、商品变体等)
  • category/: 分类聚合(包含分类层次结构)
  • brand/: 品牌聚合(包含品牌授权等)

4. 完整的DDD分层

每个限界上下文都包含:

  • Domain Layer: 聚合、实体、值对象、领域服务
  • Application Layer: 应用服务、命令/查询处理器
  • Infrastructure Layer: 持久化、外部服务适配
  • Presentation Layer: API控制器、DTO

5. 依赖方向

Presentation → Application → Domain ← Infrastructure

依赖始终指向内层,符合洋葱架构原则。