- Add comprehensive docs/ directory with standardized structure - Include architecture specification and contract documents - Add prompt templates for development workflows - Establish M1 deliverables as frozen specifications Co-authored-by: Qwen-Coder <[email protected]>
2.4 KiB
2.4 KiB
P0_system_rules.md - 宪法级规则
核心架构原则
1. 依赖方向(不可违反)
api → application → domain
↑
infrastructure ──┘
- domain 不依赖任何其他模块,不依赖任何框架
- application 只依赖 domain
- infrastructure 实现 application 中定义的 Port 接口
- api 负责组装所有模块
2. 安全上下文规则
可信来源原则
- customerId/orgId/userId 的值只能来源于网关注入的 Header
- 禁止从请求 DTO 中读取这些字段作为业务查询条件
- Controller/DTO 中禁止定义 customerId/orgId/userId 字段
多租户隔离
- 所有数据查询必须携带 customerId 作为过滤条件
- 写入操作:create_user_id/update_user_id 从安全上下文自动填充
3. 事务边界规则
- @Transactional 唯一允许位置:application 层(UseCase 或 AppService)
- domain/infrastructure/api 层禁止声明事务
- 事务内禁止调用外部 HTTP 接口或发送 MQ 消息
4. 异常处理规则
- 所有业务异常必须继承 DomainException 并携带 ErrorCode
- 禁止直接抛出 RuntimeException 表达业务规则
- ErrorCode → HTTP Status 有固定映射关系
5. 禁止模式清单
domain 层禁止
- 所有 Spring 注解
- 所有 JPA 注解
- 所有第三方框架类型
- @Autowired 字段注入
application 层禁止
- Spring MVC 类型(RestController, RequestMapping 等)
- JPA 类型(JpaRepository, @Entity 等)
- Cache 类型(@Cacheable, RedisTemplate 等)
- Messaging 类型(KafkaTemplate, @RabbitListener 等)
- Security 类型(@PreAuthorize, SecurityFilterChain 等)
infrastructure 层禁止
- 包含业务规则判断(业务逻辑只能在 domain 层)
api 层禁止
- 直接调用 Repository/Adapter(必须经过 application 层 UseCase)
6. 命名规范摘要
UseCase 入口方法
- 统一命名:execute(...)
包结构
- domain/: entity, valueobject, event, exception, repository, service
- application/: usecase, port(in/out), service, assembler
- infrastructure/: persistence, cache, messaging, external, config
- api/: controller, dto, mapper, filter, exception, config
类命名示例
- Domain Entity: Order, OrderItem
- Value Object: Money, OrderStatus
- UseCase: CreateOrderUseCase
- Command: CreateOrderCommand
- Controller: OrderController
- DTO: CreateOrderRequest