Back to skills
SKILL.md
Ddk Add Aggregate
ASecurityAdd a new aggregate to this DDK four-layer service - typed identifier, aggregate root, repository port and implementation, PO, MyBatis-Plus mapper, both converters and the table. Use when the user asks for a new domain concept that has its own identity and lifecycle, such as an order, a product or an account.
- 12 stars
- 0 votes
- 0 copies
- 0 views
- Added October 23, 2026
Security analysis
100/100npx -y skills add poppycoderr/domain-driven-kit --skill ddk-add-aggregate --agent claude-codeAre you the author of Ddk Add Aggregate?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/poppycoderr-ddk-add-aggregate)---
name: ddk-add-aggregate
description: Add a new aggregate to this DDK four-layer service - typed identifier, aggregate root, repository port and implementation, PO, MyBatis-Plus mapper, both converters and the table. Use when the user asks for a new domain concept that has its own identity and lifecycle, such as an order, a product or an account.
---
#set( $h2 = '##' )
# 新增聚合
以聚合 `Order` 为例,根包 `${package}`。把 `Order` 换成实际名称。
$h2 1. 先确认边界
- 它有独立的身份和生命周期吗?没有的话应该是值对象,不是聚合。
- 列出不变量(任何时候都必须成立的规则)和会发生的状态变化,它们决定聚合的方法。
- 其他聚合只通过标识引用它,不持有对象引用。
$h2 2. 领域层(不得引入 Spring、MyBatis、Jackson)
| 文件 | 要点 |
|---|---|
| `domain/model/OrderId.java` | `final class OrderId extends Identifier<Long>`,私有构造器校验取值,提供 `static of(Long)` |
| `domain/model/Order.java` | `extends AggregateRoot<OrderId>`;私有无参构造器;`static create(OrderId id, ...)` 调用 `assignId(id)`、校验不变量并 `registerEvent(...)`,事件带上 `id`;`static restore(..., Long version)` 调用 `assignId` 与 `assignVersion`,不登记事件;状态变更方法守卫不变量;只暴露读方法,不写 setter |
| `domain/model/*.java` | 值对象用 record 实现 `ValueObject`,在紧凑构造器里校验 |
| `domain/error/OrderError.java` | `enum OrderError implements ErrorCode`,文案用 `{0}` 占位 |
| `domain/acl/OrderRepository.java` | `interface OrderRepository extends GenericRepository<Order, OrderId>`,用类型化标识;只声明领域真正需要的查询 |
| `domain/acl/OrderIdGenerator.java` | `interface OrderIdGenerator { OrderId nextId(); }`,应用服务在创建聚合之前取号 |
| `domain/event/OrderCreatedEvent.java` | record 实现 `DomainEvent`,带 `OrderId` 和 `Instant occurredOn`,另提供一个不带 `occurredOn` 的构造器,内部用 `Instant.now()`;`DomainEvent` 要求实现 `occurredOn()`,缺了编译不过。详见 `ddk-add-domain-event` |
违反不变量时抛 `new BusinessException(OrderError.XXX, 参数)`。
$h2 3. 基础设施层
| 文件 | 要点 |
|---|---|
| `infrastructure/orm/po/OrderPO.java` | 表的镜像,Lombok `@Data`;主键 `@TableId(type = IdType.INPUT)`,ID 由 `OrderIdGenerator` 预先生成;版本列 `@Version private Long version` |
| `infrastructure/orm/mapper/OrderMapper.java` | `interface OrderMapper extends BaseMapper<OrderPO>`,加 `@Mapper` |
| `infrastructure/acl/impl/SnowflakeOrderIdGenerator.java` | `@Component` 实现 `OrderIdGenerator`,注入 MyBatis-Plus 的 `IdentifierGenerator`(DDK MyBatis starter 已注册,按 `ddk.mybatis.worker-id` / `datacenter-id` 生成雪花 ID),返回 `OrderId.of(idGenerator.nextId(null).longValue())`。不要直接调用 Hutool 的 `IdUtil`,它不读这两个配置 |
| `infrastructure/converter/OrderPoConverter.java` | `@Component` + `@EnhancedMapper(source = Order.class, target = OrderPO.class)`,实现 `BeanMapper<Order, OrderPO>`,手写字段映射,带上 `version` |
| `infrastructure/converter/OrderEntityConverter.java` | 反方向,经由 `Order.restore(...)` 重建 |
| `infrastructure/acl/impl/OrderRepositoryImpl.java` | `@Repository`,`extends GenericRepositoryImpl<Order, OrderId, OrderPO, OrderMapper> implements OrderRepository`(`OrderId` 由通用仓储拆成主键值);自定义查询用 `Wrappers.lambdaQuery(OrderPO.class)`,结果经 `toEntity()::map` 转换 |
为什么预先取号,而不是让 MyBatis-Plus 在插入时生成(`IdType.ASSIGN_ID`):插入时才有 ID 的话,`create` 里登记的事件拿不到聚合的 ID,转换器也要处理 `id()` 为 null 的情况。预先取号让聚合从诞生起就有身份。
两个方向的转换器缺一个,启动或首次使用时就会抛 `MissingMapperException`。
$h2 4. 表结构
在 `src/main/resources/db/migration` 新增一个 Flyway 迁移脚本建表,文件名取下一个版本号,例如 `V2__create_order.sql`:主键 `BIGINT`、`version BIGINT NOT NULL DEFAULT 0`、`create_time` 与 `update_time`。已经存在的迁移脚本不要修改,改表结构时再加一个新版本。
$h2 5. 测试与验证
- 为聚合写纯单元测试:工厂方法登记了事件、非法输入抛出对应错误码、状态变更守住不变量。不需要启动 Spring。用 `ddk-test` 的 `DdkAssertions`:`assertThat(order).hasRaisedExactly(OrderPlacedEvent.class)`、`assertThatRejected(order::cancel).withCode(OrderError.ORDER_NOT_CANCELLABLE)`。
- 运行 `mvn verify`,`ArchitectureTest` 必须通过。
Attribution
Related skills
View all in ai-agents →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.