ddd-factory
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOne-line summary
一句话总结
A factory encapsulates the knowledge needed to create a complete, valid aggregate or value object when construction itself is complex or has domain meaning.
当构造本身较为复杂或具有 domain 含义时,factory 封装创建完整且有效的 aggregate 或 value object 所需的知识。
When to use this skill
何时使用此 skill
- Creation requires assembling multiple sub-objects (a new is born with line items, an initial status, a created-at timestamp).
Order - Creation must enforce invariants beyond simple field validation (a new must reference a valid
Subscriptionand a non-cancelledPlan).Customer - The act of creation has a domain-meaningful name in the ubiquitous language (,
Order.placeFor(customer, items)) — not justAccount.openFor(applicant).new Order(...)
- 创建过程需要组装多个子对象(例如,新的 诞生时需要包含 line items、初始 status 和 created-at timestamp)。
Order - 创建过程必须强制执行简单字段验证之外的 invariants(例如,新的 必须引用有效的
Subscription和未取消的Plan)。Customer - 创建行为在 ubiquitous language 中具有符合领域含义的名称(、
Order.placeFor(customer, items)),而不只是Account.openFor(applicant)。new Order(...)
When NOT to use this skill
何时不应使用此 skill
- Simple objects whose constructor fully captures the work.
- A static creation method on the entity itself is sufficient — promote to a separate factory class only when the creation logic doesn't belong on the resulting object, or when multiple aggregates collaborate during creation.
- The "factory" you are tempted to write would just call — that is ceremony, not encapsulation.
new
- constructor 已能完整表达创建工作的简单对象。
- entity 自身的 static creation method 已经足够——只有当创建逻辑不属于最终对象,或者创建过程中涉及多个 aggregate 协作时,才应提升为独立的 factory class。
- 你想编写的“factory”只是调用 ——这属于形式化仪式,而非封装。
new
Core content
核心内容
Three common shapes, from least to most ceremony:
Static creation method on the entity. . Preferred when the creation logic naturally belongs on the resulting type. Names itself in the ubiquitous language. Encapsulates whatever construction work is needed without inventing a new class.
Order.placeFor(customer, items)Dedicated factory class. A with a method . Use when creation involves orchestration that doesn't belong on the resulting entity — for example, calling other aggregates, looking up reference data, or coordinating multiple inputs.
SubscriptionFactorycreateFor(applicant, plan, paymentMethod)Builder. A fluent builder for incremental construction. Use sparingly; builders shine when an aggregate has many optional fields and validation must run at the final step. Make sure the builder cannot hand back an invalid intermediate state — returns a fully validated aggregate or fails.
build()build()A factory's responsibility ends at returning a valid aggregate. Persistence is the repository's job. Mixing creation and persistence inside one factory blurs the layers.
常见的三种形式,按额外手续从少到多排列:
entity 上的 static creation method。 。当创建逻辑自然属于最终类型时,这是首选方案。它使用 ubiquitous language 命名,并封装所需的各项构造工作,无需引入新 class。
Order.placeFor(customer, items)专用 factory class。 例如,包含 方法的 。当创建过程涉及不属于最终 entity 的 orchestration 时使用,例如调用其他 aggregate、查询 reference data 或协调多个输入。
createFor(applicant, plan, paymentMethod)SubscriptionFactoryBuilder。 用于增量式构造的 fluent builder。应谨慎使用;builder 适用于 aggregate 包含许多可选字段,并且验证必须在最后的 步骤执行的情况。确保 builder 不会返回无效的中间状态—— 必须返回经过完整验证的 aggregate,否则直接失败。
build()build()factory 的职责止于返回一个有效的 aggregate。持久化 persistence 是 repository 的职责。在同一个 factory 中混合创建与持久化会模糊各层之间的边界。
Decision heuristics
决策启发式
- Start with a constructor or a static method. Promote to a factory class only when the work is too complex for the entity to own or involves multiple collaborators.
- The factory's method names should belong to the ubiquitous language — ,
placeOrder,openAccount— notissueRefund,create,make.instantiate - If a factory needs to read or persist data, you are mixing concerns — the factory probably belongs in the application layer, or it should accept already-loaded inputs.
- 从 constructor 或 static method 开始。只有当工作量过于复杂、不适合由 entity 负责,或者涉及多个协作者时,才提升为 factory class。
- factory 的方法名称应属于 ubiquitous language——例如 、
placeOrder、openAccount——而不应使用issueRefund、create、make。instantiate - 如果 factory 需要读取或持久化数据,就说明你混合了不同职责——factory 很可能应位于 application layer,或者它应接收已经加载完成的输入。
Anti-patterns
反模式
- Factories that also persist — confuses creation with lifecycle ownership.
- A class per entity, reflexively, even when its
FooFactorydoes nothing the constructor couldn't.create() - "FactoryFactory" indirection — almost always a sign of an over-engineered codebase.
- Builders that ship intermediate state — callers can hold a half-built and call methods on it.
Order
- 同时负责持久化的 factory——混淆了创建与 lifecycle ownership。
- 不加判断地为每个 entity 都创建一个 class,即使其
FooFactory并没有执行 constructor 无法完成的工作。create() - “FactoryFactory”间接层——几乎总是代码库过度设计的迹象。
- 会返回中间状态的 builder——调用方可能持有一个仅构建了一半的 并调用其方法。
Order
See also
另请参阅
- — what the factory typically creates.
ddd-entity - — factories produce whole aggregates, not half-aggregates.
ddd-aggregate - — the boundary; factory creates, repository persists.
ddd-repository
- ——factory 通常创建的对象。
ddd-entity - ——factory 生成完整的 aggregate,而不是处于半构建状态的 aggregate。
ddd-aggregate - ——两者之间的边界;factory 负责创建,repository 负责持久化。
ddd-repository
References
参考资料
- Evans, Domain-Driven Design (2003), Ch. 6 (The Life Cycle of a Domain Object).
- Evans,《Domain-Driven Design》(2003),第 6 章(The Life Cycle of a Domain Object)。