module-decompositionlisted
Install: claude install-skill structure-projects/structure-agent-rules
# 模块拆分(DDD / 微服务)
> 按 DDD 或单体规范拆分模块。**MUST 先识别 bounded context,禁止凭直觉拆分**。
## 前置条件
- 已有变更提案(`changes/proposals/<current>/proposal.md`)
- 明确项目形态(DDD 7+1 / 单体 4 模块)
## 执行步骤
### 第 1 步:识别 Bounded Context(MUST)
通过业务分析识别限界上下文:
```
业务领域
├─ 用户上下文(User Context):用户 / 组织 / 角色 / 权限
├─ 订单上下文(Order Context):订单 / 订单项 / 支付
├─ 商品上下文(Product Context):商品 / 类目 / 库存
└─ ...
```
**关键问题**(MUST 与用户确认):
- 业务边界在哪里?
- 哪些概念属于同一上下文?
- 上下文之间如何通信(同步 / 异步 / 共享数据库)?
### 第 2 步:确定拆分粒度
| 粒度 | 说明 | 适用 |
|---|---|---|
| **粗粒度** | 1 个上下文 = 1 个服务 | 小型项目 |
| **中粒度** ⭐ | 1 个上下文 = 1 个服务,内部 7+1 模块 | 中型项目(推荐) |
| **细粒度** | 1 个上下文拆为多个服务 | 大型项目 |
### 第 3 步:生成模块结构
#### DDD 7+1 多模块(推荐用于新业务中心)
```
structure-{X}/
├── structure-{X}-dependencies/ # 父 POM
├── structure-{X}-common/ # DTO / VO / Query / enums / exception
├── structure-{X}-domain/ # Entity / Repository 接口 / DomainService
├── structure-{X}-infra/ # RepositoryImpl / RepositoryDelegate
├── structure-{X}-repository-mybatis/ # PO / Mapper / MybatisPlusDelegate / Flyway
├── structure-{X}-application/ # I{X}Service / {X}ServiceImpl / {X}Assembler
├── structure-{X}-interfaces/ # Controller(api/ + open/)
└── structure-{X}-boot/ # 启动类 + application.yaml
```
**模块依赖方向**(MUST 遵守):
```
common → domain → infra → repository-mybatis
↑
application → domain + infra
interfaces → application
boot → all
```
#### 单体 4 模块(老项目 / 小型项目)
```
structure-{X}/
├── {X}-api