ddd-tactical-patterns
将 DDD 战术模式应用于您的代码
正在为如何将领域规则转化为清晰的代码结构而苦恼?本技能将指导您了解 DDD 战术模式,包括实体、值对象、聚合、仓库和领域事件。
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“ddd-tactical-patterns”。 Model an Order aggregate with submit and cancel operations
预期结果:
class Order with private status field, submit() method that validates items count and enforces single-transition invariant, and cancel() method with appropriate guard conditions. Includes domain event emission for OrderSubmitted and OrderCancelled.
正在使用“ddd-tactical-patterns”。 Create a value object for Email address
预期结果:
Immutable Email class with regex validation in constructor, equality by value comparison, and toString() method. Rejects invalid formats at construction time.
安全审计
安全All static analysis findings are false positives. The 'external_commands' detections were Markdown backticks around code examples (e.g., `OrderSubmitted`), not actual shell execution. The 'blocker' findings for weak cryptography and system reconnaissance were triggered by documentation text, not executable code. This is a safe educational skill containing only DDD pattern guidance and TypeScript examples.
质量评分
你能构建什么
重构贫血的领域模型
将仅包含数据的类转换为强制执行业务规则的富含行为的领域对象。
设计新聚合
使用适当的聚合边界和不变量强制执行来构建新的领域代码。
实现领域事件
使用过去时态的领域事件在聚合之间添加事件驱动的通信。
试试这些提示
我需要建模一个 Order 聚合。帮助我识别不变量,设计带有行为方法的实体,并为该领域定义适当的值对象。
创建一个不可变的 Money 值对象,包含 currency 和 amount。在构造时包含验证,并按值进行相等性比较。
为 Order 聚合设计一个仓库接口,暴露领域友好的查询方法,而不泄露 ORM 实现细节。
帮助我为 Order 状态转换定义领域事件。包括过去时态命名、最小化负载和模式版本控制策略。
最佳实践
- 首先识别不变量,然后围绕它们设计聚合边界
- 保持值对象不可变,在构造时进行验证
- 使用过去时态命名领域事件,负载最小且稳定
避免
- 跨聚合同步一致性规则违反了事务边界
- 将 ORM 实体泄露到领域层
- 控制器包含领域业务逻辑而非领域对象