feat: Enhance documentation on layering, object naming conventions, and API design
- Added object naming conventions (PO/DAO/BO/DTO/VO) in 02-layering.md to clarify terminology and usage within the team. - Updated 06-api-design.md to include MapStruct for DTO and entity conversion, providing examples and configuration details. - Expanded 07-config-governance.md with local development instructions and strategies for running without K8s, including two recommended approaches. - Included K8s probe configuration details in 08-observability.md for liveness and readiness checks. - Clarified CI/CD processes in 09-build-deploy.md, detailing environment distinctions and deployment strategies for local, Dev, UAT, and Prod. - Introduced ArchUnit for architectural testing in 10-testing.md, ensuring adherence to defined layering rules and coverage verification with Jacoco.
This commit is contained in:
@@ -96,6 +96,37 @@ data class StoreResponse(
|
||||
|
||||
Controller 不直接返回 `StoreEntity`,而是转换成 `StoreResponse`——即使当前字段一模一样,也统一走这层转换,避免以后 entity 加了内部字段(比如某个只有 `infrastructure` 层需要的标记位)被不小心带出去。
|
||||
|
||||
## DTO 与 entity 的转换:用 MapStruct
|
||||
|
||||
转换代码用 [MapStruct](https://mapstruct.org/) 自动生成,不手写。字段名一致的直接映射,不一致的用 `@Mapping` 指定,编译期生成实现类,没有反射开销,字段漏映射编译期就能发现。
|
||||
|
||||
```groovy
|
||||
// build.gradle(Kotlin 项目用 kapt 做注解处理)
|
||||
plugins {
|
||||
id 'org.jetbrains.kotlin.kapt'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.mapstruct:mapstruct:1.6.3'
|
||||
kapt 'org.mapstruct:mapstruct-processor:1.6.3'
|
||||
}
|
||||
```
|
||||
|
||||
```kotlin
|
||||
// api/mapper/StoreMapper.kt
|
||||
@Mapper(componentModel = "spring")
|
||||
interface StoreMapper {
|
||||
fun toResponse(entity: StoreEntity): StoreResponse
|
||||
|
||||
@Mapping(target = "displayName", source = "name")
|
||||
fun toSummary(entity: StoreEntity): StoreSummaryResponse // 字段名不一致时用 @Mapping 指定
|
||||
|
||||
fun toResponseList(entities: List<StoreEntity>): List<StoreResponse>
|
||||
}
|
||||
```
|
||||
|
||||
`componentModel = "spring"` 让 MapStruct 生成的实现类自动注册成 Spring bean,直接在 `application` 层注入 `StoreMapper` 使用,不用手动 `new`。
|
||||
|
||||
## 关键规则
|
||||
|
||||
- Controller 不直接返回 entity,统一走 `Xxx*Response` DTO,避免持久层字段变更影响 API 契约。
|
||||
@@ -115,7 +146,6 @@ Controller 不直接返回 `StoreEntity`,而是转换成 `StoreResponse`——
|
||||
|
||||
## 待补充
|
||||
|
||||
- DTO 与 entity 的转换方式(MapStruct 还是手写 mapper,模块变多之后再评估)。
|
||||
- 分页/排序参数的统一约定。
|
||||
- 错误码表(按 domain 分段还是全局统一编码)。
|
||||
|
||||
@@ -124,3 +154,4 @@ Controller 不直接返回 `StoreEntity`,而是转换成 `StoreResponse`——
|
||||
- [springdoc-openapi](https://springdoc.org/)
|
||||
- [Spring 官方 Bean Validation 指南](https://docs.spring.io/spring-framework/reference/core/validation/beanvalidation.html)
|
||||
- [Microsoft REST API 设计指南](https://github.com/microsoft/api-guidelines)
|
||||
- [MapStruct 官方文档](https://mapstruct.org/documentation/stable/reference/html/)
|
||||
|
||||
Reference in New Issue
Block a user