注意:所有方法都需要做注释!!!本规范与 dev-spec.md(B 轨)并列;判轨见 ddd-qizh skill(信号 ≥2 分走 A 轨)。
db/api/i18n/时间/统一响应等通用规范一律遵循既有 specs(db-spec.md、ui-api.md、dev-i18n.md、dev-spec.md §14-15)。
┌─────────────────────────────────────────────────────────────┐
│ Controller 层(按 dev-spec §5) │
│ 接收HTTP请求 → 转 Cmd → 调应用服务 → 返回 Result │
├─────────────────────────────────────────────────────────────┤
│ Application 层(应用服务) │
│ 职责:编排(load → 领域方法 → save),无业务规则;发事务事件 │
├─────────────────────────────────────────────────────────────┤
│ Domain 层(聚合) │
│ 聚合根/实体/值对象/领域事件/仓储接口/领域异常 │
│ 【零框架依赖】不 import Spring/MyBatis-Plus/Web 任何类 │
├─────────────────────────────────────────────────────────────┤
│ Infrastructure 层 │
│ PO/Mapper/Repository实现(按dev-spec §7-8)/Converter/监听器 │
└─────────────────────────────────────────────────────────────┘
cn.com.qizh.itsm.{module}/
├── controller/ # 按 dev-spec §5
│ └── {Module}Controller.java
├── application/
│ ├── {Module}ApplicationService.java
│ ├── command/ # Cmd(入参,不可变)
│ │ └── {Action}Cmd.java
│ └── query/ # 查询走 B 轨风格(可直接用 BO+Repository)
├── domain/
│ ├── {Aggregate}.java # 聚合根
│ ├── {Entity}.java # 聚合内实体(可选)
│ ├── {Concept}.java # 值对象(record)
│ ├── event/
│ │ └── {Aggregate}{Event}.java # 领域事件(record)
│ ├── repository/
│ │ └── {Aggregate}Repository.java # 仓储接口
│ └── exception/
│ └── {Aggregate}DomainException.java
└── infrastructure/
├── persistence/ # 全按 dev-spec:PO/Mapper/Repository实现
│ ├── po/{Table}.java
│ ├── mapper/{Table}Mapper.java
│ ├── repository/{Aggregate}RepositoryImpl.java
│ └── convert/{Aggregate}Convert.java
└── listener/
└── {Aggregate}EventListener.java
| 类型 | 规则 | 示例 |
|---|---|---|
| 聚合根 | 业务名词,无后缀 | Incident |
| 值对象 | 概念名词 record | TicketNo、PriorityLevel |
| 领域事件 | {Aggregate}{过去式事件},与设计档文件名一致 |
IncidentAssigned |
| 应用服务 | {Module}ApplicationService |
IncidentApplicationService |
| Cmd | {动作}Cmd(对应事件墙命令) |
AssignIncidentCmd |
| 领域异常 | {Aggregate}DomainException |
IncidentDomainException |
| 仓储 | 接口 {Aggregate}Repository / 实现 {Aggregate}RepositoryImpl |
— |
/**
* 故障报修工单(聚合根)。不变量清单见 docs/design/domain-model/aggregates.md
*/
public class Incident {
private Long id; // 聚合根可用 Long id(PO 主键),对外语义用 TicketNo
private TicketNo ticketNo; // 值对象,创建后不可变(INV-1)
private IncidentStatus status; // 强类型枚举
private Long assigneeId;
private final List<Object> events = new ArrayList<>(); // 待发布领域事件
/** 工厂:校验不变量后才能诞生(INV 编号注释必写) */
public static Incident create(CreateIncidentCmd cmd) {
// INV-1: 工单号全局唯一 —— 由仓储 generateTicketNo() 保证,此处校验非空格式
if (cmd.ticketNo() == null) {
throw new IncidentDomainException(IncidentErrorCode.TICKET_NO_REQUIRED);
}
Incident incident = new Incident();
incident.ticketNo = cmd.ticketNo();
incident.status = IncidentStatus.DRAFT;
incident.record(new IncidentCreated(incident.ticketNo.value(), Instant.now()));
return incident;
}
/**
* 派单(INV-3: 已派单未接单不可改派,可配置)
*/
public void assign(AssigneeId to) {
if (status == IncidentStatus.ASSIGNED) {
throw new IncidentDomainException(IncidentErrorCode.REASSIGN_NOT_ALLOWED); // INV-3
}
this.assigneeId = to.value();
record(new IncidentAssigned(id, to.value(), Instant.now()));
}
/** 仅聚合内部/测试可读;不提供 setStatus/setAssignee 暴露 */
public IncidentStatus getStatus() { return status; }
/** 待发布事件(仓储 save 后由应用服务统一发布) */
public List<Object> drainEvents() {
var out = List.copyOf(events);
events.clear();
return out;
}
private void record(Object event) { events.add(event); }
}
规则:构造器私有,只能走工厂/领域方法;无 setter;每个领域方法 javadoc 标 INV-n;一个方法一件业务事,产生一个事件。
/** 已派单。载荷字段与时序档契约卡的载荷表一一对应 */
public record IncidentAssigned(
Long ticketId,
Long assigneeId,
Instant occurredAt // 时间规范:Instant,见 dev-spec §14
) {}
domain 层不得依赖 BusinessException(框架类)。自定义领域异常 + 全局转译:
/** 领域不变量违反。code 与状态档迁移表"动作"列错误码一致(如 ITSMD_4012) */
public class IncidentDomainException extends RuntimeException {
private final String code; // ITSMD_xxxx
public IncidentDomainException(String code) { this.code = code; }
public String getCode() { return code; }
}
infrastructure 提供 @RestControllerAdvice 转译:Result.fail(e.getCode(), messageKey);messageKey 走 dev-i18n({Module}Code + messages_*.properties)。
/** 工单聚合仓储(domain 层,零框架依赖) */
public interface IncidentRepository {
Incident load(Long id); // 不存在抛领域异常
void save(Incident incident); // Converter 双向映射 PO
TicketNo nextTicketNo(); // 支撑 INV-1
}
@Repository
public class IncidentRepositoryImpl extends ServiceImpl<IncidentMapper, IncidentPO>
implements IncidentRepository {
@Resource private IncidentConvert convert;
/** 加载聚合:PO → 聚合 */
public Incident load(Long id) {
IncidentPO po = this.getById(id);
if (po == null || po.getDelFlag() == 1) {
throw new IncidentDomainException(IncidentErrorCode.NOT_FOUND);
}
return convert.toAggregate(po);
}
/** 保存聚合:聚合 → PO(全字段按 dev-spec db 规范;更新 update_time/update_by) */
public void save(Incident incident) { /* convert.toPO + saveOrUpdate */ }
}
@Service
@Slf4j
public class IncidentApplicationService {
@Resource private IncidentRepository incidentRepository;
@Resource private ApplicationEventPublisher publisher;
/**
* 派单:加载→领域方法→保存→发布事件(对应时序档 IncidentAssigned ②-⑤段)
*/
@Transactional(rollbackFor = Exception.class)
public void assign(AssignIncidentCmd cmd) {
Incident incident = incidentRepository.load(cmd.ticketId()); // ②
incident.assign(new AssigneeId(cmd.assigneeId())); // ③④(⑤收集)
incidentRepository.save(incident); // 持久化
incident.drainEvents().forEach(publisher::publishEvent); // ⑤事务内登记
}
}
事件监听(⑥⑦段,事务提交后):
@Component
public class IncidentEventListener {
/** 派单后通知——AFTER_COMMIT,不拖事务;失败重试走时序档⑧约定 */
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void on(IncidentAssigned e) {
notifyService.push(e.assigneeId(), e.ticketId());
workloadService.increase(e.assigneeId());
}
}
completeTask)onNodeCompleted(node) 后 drain 发布)、平台通知。**禁止**:业务规则校验、跨聚合写操作——规则属聚合,塞进监听器即破坏"规则在前流程在后"warmFlowClient.completeTask(taskId, vars),在流程监听器(FlowListener)回调里调聚合的领域方法(如 incident.onNodeCompleted(node))并发布对应事件status = X 只许在流程回调适配处发生一次)/** 聚合不变量单测:纯 JVM,不起 Spring(对应 INV-3) */
@Test
void 已派单未接单不可改派() {
Incident incident = Incident.create(TestCmds.createIncident());
incident.assign(new AssigneeId(1L));
assertThatThrownBy(() -> incident.assign(new AssigneeId(2L)))
.isInstanceOf(IncidentDomainException.class)
.extracting("code").isEqualTo("ITSMD_4012");
}
| 设计产物 | 测试 |
|---|---|
| 不变量 INV-n | 聚合 JVM 单测(每条至少 1 正 1 反) |
| 时序档 | 集成测试(含 alt 失败分支与补偿) |
| 状态档非法迁移行 | 拒绝用例(断言 code + 状态未变) |