Skip to content
Back to skills

Ddk Add Feature

ASecurity

Add a business feature to this DDK three-layer service - entity, repository contract and implementation, PO, mapper, converters, business service, request, response and controller endpoint. Use when the user asks for a new capability such as "manage coupons" or "add an endpoint to freeze an account".

  • 12 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 23, 2026
ai-agentsjavasqlspringapi

Works with

  • api

Security analysis

A100/100

Scanned November 26, 2026

npx -y skills add poppycoderr/domain-driven-kit --skill ddk-add-feature --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Ddk Add Feature?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Ddk Add Feature
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/poppycoderr-ddk-add-feature/badge)](https://www.skillsdirectory.com/skills/poppycoderr-ddk-add-feature)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: ddk-add-feature
description: Add a business feature to this DDK three-layer service - entity, repository contract and implementation, PO, mapper, converters, business service, request, response and controller endpoint. Use when the user asks for a new capability such as "manage coupons" or "add an endpoint to freeze an account".
---
#set( $h2 = '##' )

# 新增业务功能

以「优惠券 Coupon」为例,根包 `${package}`。已有实体时跳过第 1、3 步,只补用例。

$h2 1. 业务模型(business.model / error / acl)

| 文件 | 要点 |
|---|---|
| `business/model/Coupon.java` | 可继承 `AggregateRoot<CouponId>`;私有构造器;创建工厂方法接收预先生成的 `CouponId` 并 `assignId`,校验规则并登记事件(事件带上 ID),重建工厂方法不登记;状态变更方法守卫规则;不写公开 setter |
| `business/model/CouponId.java` | `final class CouponId extends Identifier<Long>`,提供 `static of(Long)` |
| `business/error/CouponError.java` | `enum CouponError implements ErrorCode` |
| `business/acl/CouponRepository.java` | `interface CouponRepository extends GenericRepository<Coupon, CouponId>` |
| `business/acl/CouponIdGenerator.java` | `interface CouponIdGenerator { CouponId nextId(); }`,业务服务在创建实体之前取号 |
| `business/event/CouponIssuedEvent.java` | 需要事件时:record 实现 `DomainEvent`,带 `Instant occurredOn`,另提供一个不带它、内部用 `Instant.now()` 的构造器;`DomainEvent` 要求实现 `occurredOn()` |

$h2 2. 业务服务(business.command / response / service)

- `*Command` 与 `*Query` 用 record;分页查询继承 `PageQuery`。
- `CouponResponse` 用 record,提供 `static from(Coupon)`。
- `CouponService`:`@Service`,写操作加 `@Transactional`;取实体 → 调用实体方法 → 保存 → 转响应。只依赖 `CouponRepository` 接口,不注入 `CouponMapper` 或任何实现类。

$h2 3. 基础设施(infrastructure)

| 文件 | 要点 |
|---|---|
| `infrastructure/orm/po/CouponPO.java` | `@Data`,`@TableId(type = IdType.INPUT)`(ID 由 `CouponIdGenerator` 预先生成),`@Version private Long version` |
| `infrastructure/acl/impl/SnowflakeCouponIdGenerator.java` | `@Component` 实现 `CouponIdGenerator`,注入 MyBatis-Plus 的 `IdentifierGenerator`(DDK MyBatis starter 已注册,按 `ddk.mybatis.worker-id` / `datacenter-id` 生成雪花 ID),返回 `CouponId.of(idGenerator.nextId(null).longValue())`;不要直接调用 Hutool 的 `IdUtil`,它不读这两个配置 |
| `infrastructure/orm/mapper/CouponMapper.java` | `@Mapper interface CouponMapper extends BaseMapper<CouponPO>` |
| `infrastructure/converter/*Converter.java` | 两个方向各一个:`@Component` + `@EnhancedMapper(source = ..., target = ...)` 实现 `BeanMapper` |
| `infrastructure/acl/impl/CouponRepositoryImpl.java` | `@Repository`,`extends GenericRepositoryImpl<Coupon, CouponId, CouponPO, CouponMapper> implements CouponRepository` |

预先取号而不是插入时生成(`IdType.ASSIGN_ID`),是为了让实体从创建起就有 ID:创建时登记的事件能带上它,转换器也不用处理 ID 为 null 的情况。

在 `src/main/resources/db/migration` 新增一个 Flyway 迁移脚本建表,文件名取下一个版本号,例如 `V2__create_order.sql`:主键 `BIGINT`、`version BIGINT NOT NULL DEFAULT 0`、`create_time`、`update_time`。已经存在的迁移脚本不要修改,改表结构时再加一个新版本。

$h2 4. 接口(adapter.controller)

- `request/*Request.java`:record + Bean Validation 注解 + `toCommand()`。
- `CouponController`:`@Valid @RequestBody`、`@PathVariable("id")`、返回 `ApiResponse<T>`;只调用 `CouponService`。

$h2 5. 测试与验证

- 实体规则写纯单元测试,被规则拒绝的操作用 `ddk-test` 的 `assertThatRejected(...).withCode(...)` 断言错误码;接口用 `@SpringBootTest` + `@AutoConfigureMockMvc` 覆盖参数绑定与错误码。
- 运行 `mvn verify`,`ArchitectureTest` 必须通过。

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.