Initial commit: ai4se-harness-suite
Import harness suite (skills, installers, overnight-runner, docs) as a fresh history for InspireChina. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
# REST Controller(Adapter 层)
|
||||
|
||||
```java
|
||||
package com.xxx.adapter.rest;
|
||||
|
||||
import com.xxx.application.service.XxxService;
|
||||
import com.xxx.infrastructure.security.SecurityUtils;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/xxx")
|
||||
@RequiredArgsConstructor
|
||||
public class XxxController {
|
||||
|
||||
private final XxxService xxxService;
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<?> list() {
|
||||
return ResponseEntity.ok(xxxService.getMyXxx(SecurityUtils.currentUserId()));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<?> get(@PathVariable String id) {
|
||||
return ResponseEntity.ok(xxxService.getById(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<?> create(@Valid @RequestBody CreateRequest r) {
|
||||
return ResponseEntity.ok(xxxService.create(SecurityUtils.currentUserId(), r.field1, r.field2));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}/action")
|
||||
public ResponseEntity<Void> action(@PathVariable String id, @RequestBody Map<String, String> body) {
|
||||
xxxService.action(id, SecurityUtils.currentUserId(), body.getOrDefault("reason", "默认原因"));
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class CreateRequest {
|
||||
@NotBlank String field1;
|
||||
String field2;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- Controller 只做协议转换,不包含业务逻辑
|
||||
- 使用 `@Valid` 做参数校验,内联 DTO 类
|
||||
- 通过 `SecurityUtils.currentUserId()` 获取当前用户
|
||||
- 返回 `ResponseEntity<?>` 统一响应格式
|
||||
@@ -0,0 +1,54 @@
|
||||
# AppService(Application 层)
|
||||
|
||||
```java
|
||||
package com.xxx.application.service;
|
||||
|
||||
import com.xxx.application.assembler.XxxAssembler;
|
||||
import com.xxx.domain.aggregate.Xxx;
|
||||
import com.xxx.domain.repository.XxxRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class XxxService {
|
||||
|
||||
private final XxxRepository xxxRepo;
|
||||
private final XxxAssembler assembler;
|
||||
|
||||
public XxxAssembler.XxxResponse getById(String id) {
|
||||
return assembler.toResponse(xxxRepo.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("记录不存在")));
|
||||
}
|
||||
|
||||
public List<XxxAssembler.XxxResponse> getMyXxx(String userId) {
|
||||
return xxxRepo.findByUserId(userId, 1, 100).stream()
|
||||
.map(assembler::toResponse).toList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public XxxAssembler.XxxResponse create(String userId, String field1, String field2) {
|
||||
// 1. 校验(通过 repository 或 domain service)
|
||||
// 2. 构建聚合根
|
||||
Xxx xxx = xxxRepo.save(Xxx.builder()
|
||||
.id(UUID.randomUUID().toString())
|
||||
.userId(userId)
|
||||
.field1(field1)
|
||||
.field2(field2)
|
||||
.status(XxxStatus.PENDING)
|
||||
.build());
|
||||
return assembler.toResponse(xxx);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- `@Transactional` 标记在用例方法上,事务边界在 application 层
|
||||
- 组合 repository、domain service 完成用例编排
|
||||
- 使用 assembler 将 domain 转为 DTO 返回给调用方(controller)
|
||||
- 异常使用 `IllegalArgumentException`(400)或 `IllegalStateException`(409)
|
||||
40
example-project/docs/standards/templates/03-ddd-assembler.md
Normal file
40
example-project/docs/standards/templates/03-ddd-assembler.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# Assembler(Domain → DTO 转换)
|
||||
|
||||
```java
|
||||
package com.xxx.application.assembler;
|
||||
|
||||
import com.xxx.domain.aggregate.Xxx;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class XxxAssembler {
|
||||
|
||||
public XxxResponse toResponse(Xxx xxx) {
|
||||
if (xxx == null) return null;
|
||||
return XxxResponse.builder()
|
||||
.id(xxx.getId())
|
||||
.userId(xxx.getUserId())
|
||||
.field1(xxx.getField1())
|
||||
.field2(xxx.getField2())
|
||||
.status(xxx.getStatus().name())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Data @Builder
|
||||
public static class XxxResponse {
|
||||
String id;
|
||||
String userId;
|
||||
String field1;
|
||||
String field2;
|
||||
String status;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- Assembler 负责 Domain → DTO 的转换,在 Application 层
|
||||
- Converter(Infrastructure 层)负责 Domain ⟷ PO 的转换 — 见 `08-ddd-converter.md`
|
||||
- DTO 使用 `@Data @Builder`,内联为静态类
|
||||
57
example-project/docs/standards/templates/04-ddd-aggregate.md
Normal file
57
example-project/docs/standards/templates/04-ddd-aggregate.md
Normal file
@@ -0,0 +1,57 @@
|
||||
# 聚合根(Domain 层)
|
||||
|
||||
```java
|
||||
package com.xxx.domain.aggregate;
|
||||
|
||||
import com.xxx.domain.vo.XxxStatus;
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Value @Builder
|
||||
public class Xxx {
|
||||
String id;
|
||||
String userId;
|
||||
String field1;
|
||||
String field2;
|
||||
LocalDate date;
|
||||
XxxStatus status;
|
||||
String note;
|
||||
|
||||
// 业务守卫方法
|
||||
public boolean isOwnedBy(String uid) { return userId.equals(uid); }
|
||||
|
||||
public boolean canCancel() {
|
||||
return status == XxxStatus.PENDING || status == XxxStatus.CONFIRMED;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- 使用 `@Value @Builder` 实现不可变对象
|
||||
- 业务逻辑以守卫方法(guard method)形式封装在聚合根内
|
||||
- 不含框架注解,纯 Java POJO
|
||||
- 枚举实现 `Description` 接口用于数据字典注册(见 `04b-ddd-enum.md`)
|
||||
|
||||
### 示例:带枚举的聚合根
|
||||
|
||||
```java
|
||||
@Value @Builder
|
||||
public class Consultant {
|
||||
String id; String name; String title; String bio;
|
||||
String specialties; String avatar; String branchId;
|
||||
ConsultantStatus status; int sortOrder;
|
||||
|
||||
public enum ConsultantStatus implements com.xxx.domain.datadictionary.Description {
|
||||
ACTIVE("可预约"), DISABLED("不可预约");
|
||||
|
||||
private final String description;
|
||||
ConsultantStatus(String description) { this.description = description; }
|
||||
@Override public String getCode() { return name(); }
|
||||
@Override public String getDescription() { return description; }
|
||||
}
|
||||
|
||||
public boolean isAvailable() { return status == ConsultantStatus.ACTIVE; }
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,31 @@
|
||||
# Domain Service(Domain 层)
|
||||
|
||||
```java
|
||||
package com.xxx.domain.service;
|
||||
|
||||
import com.xxx.domain.aggregate.Xxx;
|
||||
import com.xxx.domain.vo.XxxStatus;
|
||||
import java.util.List;
|
||||
|
||||
public class XxxConflictService {
|
||||
|
||||
/**
|
||||
* 校验新建记录与已有记录不冲突。
|
||||
* 冲突时抛出 IllegalStateException(HTTP 409)。
|
||||
*/
|
||||
public void validateNoConflict(List<Xxx> existing, CheckParam param) {
|
||||
boolean conflict = existing.stream().anyMatch(a ->
|
||||
a.getField1().equals(param.field1()) &&
|
||||
a.getStatus() != XxxStatus.CANCELLED);
|
||||
if (conflict) throw new IllegalStateException("冲突:该条件已存在");
|
||||
}
|
||||
|
||||
public record CheckParam(String field1) {}
|
||||
}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- Domain Service 处理跨聚合的业务校验,不在任何一个聚合根内
|
||||
- 不依赖 Spring,纯 Java
|
||||
- 在 AppService 中组合使用
|
||||
@@ -0,0 +1,26 @@
|
||||
# Repository 接口(Domain 层)
|
||||
|
||||
```java
|
||||
package com.xxx.domain.repository;
|
||||
|
||||
import com.xxx.domain.aggregate.Xxx;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface XxxRepository {
|
||||
Optional<Xxx> findById(String id);
|
||||
List<Xxx> findByUserId(String userId, int page, int pageSize);
|
||||
List<Xxx> findByField1(String field1);
|
||||
List<Xxx> findAll(int page, int pageSize);
|
||||
Xxx save(Xxx xxx);
|
||||
void update(Xxx xxx);
|
||||
long count();
|
||||
}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- 接口定义在 Domain 层,实现放在 Infrastructure 层(依赖反转)
|
||||
- 方法操作的是 Domain 对象(`Xxx`),而非 PO
|
||||
- 方法命名贴近业务语言
|
||||
@@ -0,0 +1,82 @@
|
||||
# Repository 实现(Infrastructure 层 — MyBatis-Plus)
|
||||
|
||||
```java
|
||||
package com.xxx.infrastructure.repositoryimpl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.xxx.domain.aggregate.Xxx;
|
||||
import com.xxx.domain.repository.XxxRepository;
|
||||
import com.xxx.infrastructure.persistence.converter.XxxConverter;
|
||||
import com.xxx.infrastructure.persistence.mapper.XxxMapper;
|
||||
import com.xxx.infrastructure.persistence.po.XxxPO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
public class XxxRepositoryImpl implements XxxRepository {
|
||||
|
||||
private final XxxMapper mapper;
|
||||
private final XxxConverter converter;
|
||||
|
||||
@Override public Optional<Xxx> findById(String id) {
|
||||
return Optional.ofNullable(converter.toDomain(mapper.selectById(id)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Xxx> findByUserId(String userId, int page, int pageSize) {
|
||||
return mapper.selectPage(new Page<>(page, pageSize),
|
||||
new LambdaQueryWrapper<XxxPO>()
|
||||
.eq(XxxPO::getUserId, userId)
|
||||
.orderByDesc(XxxPO::getCreatedTime))
|
||||
.getRecords().stream()
|
||||
.map(converter::toDomain).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Xxx> findByField1(String field1) {
|
||||
return mapper.selectList(new LambdaQueryWrapper<XxxPO>()
|
||||
.eq(XxxPO::getField1, field1))
|
||||
.stream().map(converter::toDomain).toList();
|
||||
}
|
||||
|
||||
@Override public List<Xxx> findAll(int page, int pageSize) {
|
||||
return mapper.selectPage(new Page<>(page, pageSize), null)
|
||||
.getRecords().stream().map(converter::toDomain).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Xxx save(Xxx xxx) {
|
||||
XxxPO po = converter.toPO(xxx);
|
||||
po.setCreatedBy(xxx.getUserId());
|
||||
po.setCreatedTime(LocalDateTime.now());
|
||||
po.setUpdatedBy(xxx.getUserId());
|
||||
po.setUpdatedTime(LocalDateTime.now());
|
||||
mapper.insert(po);
|
||||
return xxx;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Xxx xxx) {
|
||||
XxxPO po = converter.toPO(xxx);
|
||||
po.setUpdatedTime(LocalDateTime.now());
|
||||
mapper.updateById(po);
|
||||
}
|
||||
|
||||
@Override public long count() {
|
||||
return mapper.selectCount(null);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- `@Repository` 标记,实现 Domain 层的接口
|
||||
- 通过 Converter 在 PO 和 Domain 之间转换
|
||||
- 使用 LambdaQueryWrapper 做类型安全的查询
|
||||
- 审计字段(createdBy/createdTime/updatedBy/updatedTime)在持久化时设置
|
||||
47
example-project/docs/standards/templates/08-ddd-converter.md
Normal file
47
example-project/docs/standards/templates/08-ddd-converter.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# Converter(Infrastructure 层 — PO ⟷ Domain 转换)
|
||||
|
||||
```java
|
||||
package com.xxx.infrastructure.persistence.converter;
|
||||
|
||||
import com.xxx.domain.aggregate.Xxx;
|
||||
import com.xxx.domain.vo.XxxStatus;
|
||||
import com.xxx.infrastructure.persistence.po.XxxPO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class XxxConverter {
|
||||
|
||||
public Xxx toDomain(XxxPO po) {
|
||||
if (po == null) return null;
|
||||
return Xxx.builder()
|
||||
.id(po.getId())
|
||||
.userId(po.getUserId())
|
||||
.field1(po.getField1())
|
||||
.field2(po.getField2())
|
||||
.date(po.getDate())
|
||||
.status(XxxStatus.valueOf(po.getStatus()))
|
||||
.note(po.getNote())
|
||||
.build();
|
||||
}
|
||||
|
||||
public XxxPO toPO(Xxx xxx) {
|
||||
if (xxx == null) return null;
|
||||
XxxPO po = new XxxPO();
|
||||
po.setId(xxx.getId());
|
||||
po.setUserId(xxx.getUserId());
|
||||
po.setField1(xxx.getField1());
|
||||
po.setField2(xxx.getField2());
|
||||
po.setDate(xxx.getDate());
|
||||
po.setStatus(xxx.getStatus().name());
|
||||
po.setNote(xxx.getNote());
|
||||
return po;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- `@Component`,两层双向转换
|
||||
- `toDomain` 使用 Domain 的 `@Builder` 构建
|
||||
- `toPO` 使用 PO 的 setter(Lombok `@Data`)
|
||||
- 枚举字段通过 `.name()` / `valueOf()` 与字符串互转
|
||||
50
example-project/docs/standards/templates/09-ddd-po-mapper.md
Normal file
50
example-project/docs/standards/templates/09-ddd-po-mapper.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# PO 与 Mapper(Infrastructure 层 — MyBatis-Plus)
|
||||
|
||||
## PO(持久化对象)
|
||||
|
||||
```java
|
||||
package com.xxx.infrastructure.persistence.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@TableName("xxx")
|
||||
public class XxxPO {
|
||||
private String id;
|
||||
private String userId;
|
||||
private String field1;
|
||||
private String field2;
|
||||
private LocalDate date;
|
||||
private String status;
|
||||
private String note;
|
||||
// 审计字段
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
}
|
||||
```
|
||||
|
||||
## Mapper
|
||||
|
||||
```java
|
||||
package com.xxx.infrastructure.persistence.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xxx.infrastructure.persistence.po.XxxPO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface XxxMapper extends BaseMapper<XxxPO> {}
|
||||
```
|
||||
|
||||
## 要点
|
||||
|
||||
- PO 使用 `@Data` + `@TableName`,字段与数据库列一一对应
|
||||
- 枚举字段存为 `String`(`status`),通过 Converter 与 Domain 枚举互转
|
||||
- Mapper 继承 `BaseMapper<PO>`,无需手写 SQL
|
||||
- 主键为 UUID 字符串(`VARCHAR(64)`),由应用层生成
|
||||
- 审计字段(createdBy/createdTime/updatedBy/updatedTime)在每个 PO 中定义
|
||||
@@ -0,0 +1,65 @@
|
||||
# 全局异常处理(Infrastructure 层)
|
||||
|
||||
```java
|
||||
package com.xxx.infrastructure.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public ResponseEntity<ErrorResponse> handleBadArg(IllegalArgumentException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("PARAM_ERROR", e.getMessage()));
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalStateException.class)
|
||||
public ResponseEntity<ErrorResponse> handleConflict(IllegalStateException e) {
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT)
|
||||
.body(new ErrorResponse("BUSINESS_CONFLICT", e.getMessage()));
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException e) {
|
||||
var msg = e.getBindingResult().getFieldError();
|
||||
return ResponseEntity.badRequest().body(
|
||||
new ErrorResponse("VALIDATION_ERROR",
|
||||
msg != null ? msg.getDefaultMessage() : "参数错误"));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleOther(Exception e) {
|
||||
log.error("Unexpected error", e);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(new ErrorResponse("SYSTEM_ERROR", "系统繁忙"));
|
||||
}
|
||||
|
||||
public record ErrorResponse(String errorCode, String errorMessage, Object data) {
|
||||
public ErrorResponse(String errorCode, String errorMessage) {
|
||||
this(errorCode, errorMessage, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 异常 → HTTP 状态码映射
|
||||
|
||||
| 异常 | HTTP | errorCode |
|
||||
|------|------|-----------|
|
||||
| `IllegalArgumentException` | 400 | `PARAM_ERROR` |
|
||||
| `MethodArgumentNotValidException` | 400 | `VALIDATION_ERROR` |
|
||||
| `IllegalStateException` | 409 | `BUSINESS_CONFLICT` |
|
||||
| `Exception`(未预期) | 500 | `SYSTEM_ERROR` |
|
||||
|
||||
## 要点
|
||||
|
||||
- 业务参数错误用 `IllegalArgumentException`,返回 400
|
||||
- 业务冲突用 `IllegalStateException`,返回 409
|
||||
- 未预期异常记录日志并返回 500,不暴露内部信息
|
||||
- 响应体统一为 `ErrorResponse` 结构
|
||||
18
example-project/docs/standards/templates/README.md
Normal file
18
example-project/docs/standards/templates/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Snippet — 常见业务代码模版
|
||||
|
||||
存放 DDD 四层架构的代码打样,供新功能开发时参考复制。所有模版均来自本项目的实现代码。
|
||||
|
||||
## 目录
|
||||
|
||||
| # | 文件 | 层 | 用途 |
|
||||
|---|------|-----|------|
|
||||
| 1 | `01-ddd-controller.md` | Adapter | REST Controller 模版 |
|
||||
| 2 | `02-ddd-appservice.md` | Application | AppService 用例编排模版 |
|
||||
| 3 | `03-ddd-assembler.md` | Application | Domain → DTO 转换器模版 |
|
||||
| 4 | `04-ddd-aggregate.md` | Domain | 聚合根模版(含业务守卫方法) |
|
||||
| 5 | `05-ddd-domain-service.md` | Domain | Domain Service 模版 |
|
||||
| 6 | `06-ddd-repository-interface.md` | Domain | Repository 接口模版 |
|
||||
| 7 | `07-ddd-repository-impl.md` | Infrastructure | MyBatis-Plus Repository 实现模版 |
|
||||
| 8 | `08-ddd-converter.md` | Infrastructure | PO ⟷ Domain 转换器模版 |
|
||||
| 9 | `09-ddd-po-mapper.md` | Infrastructure | PO 与 Mapper 模版 |
|
||||
| 10 | `10-global-exception-handler.md` | Infrastructure | 全局异常处理模版 |
|
||||
Reference in New Issue
Block a user