ddd-factory

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

One-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
    Order
    is born with line items, an initial status, a created-at timestamp).
  • Creation must enforce invariants beyond simple field validation (a new
    Subscription
    must reference a valid
    Plan
    and a non-cancelled
    Customer
    ).
  • The act of creation has a domain-meaningful name in the ubiquitous language (
    Order.placeFor(customer, items)
    ,
    Account.openFor(applicant)
    ) — not just
    new Order(...)
    .
  • 创建过程需要组装多个子对象(例如,新的
    Order
    诞生时需要包含 line items、初始 status 和 created-at timestamp)。
  • 创建过程必须强制执行简单字段验证之外的 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
    new
    — that is ceremony, not encapsulation.
  • 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.
Order.placeFor(customer, items)
. 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.
Dedicated factory class. A
SubscriptionFactory
with a method
createFor(applicant, plan, paymentMethod)
. 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.
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
build()
step. Make sure the builder cannot hand back an invalid intermediate state —
build()
returns a fully validated aggregate or fails.
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。
Order.placeFor(customer, items)
。当创建逻辑自然属于最终类型时,这是首选方案。它使用 ubiquitous language 命名,并封装所需的各项构造工作,无需引入新 class。
专用 factory class。 例如,包含
createFor(applicant, plan, paymentMethod)
方法的
SubscriptionFactory
。当创建过程涉及不属于最终 entity 的 orchestration 时使用,例如调用其他 aggregate、查询 reference data 或协调多个输入。
Builder。 用于增量式构造的 fluent builder。应谨慎使用;builder 适用于 aggregate 包含许多可选字段,并且验证必须在最后的
build()
步骤执行的情况。确保 builder 不会返回无效的中间状态——
build()
必须返回经过完整验证的 aggregate,否则直接失败。
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
    ,
    issueRefund
    — not
    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
    FooFactory
    class per entity, reflexively, even when its
    create()
    does nothing the constructor couldn't.
  • "FactoryFactory" indirection — almost always a sign of an over-engineered codebase.
  • Builders that ship intermediate state — callers can hold a half-built
    Order
    and call methods on it.
  • 同时负责持久化的 factory——混淆了创建与 lifecycle ownership。
  • 不加判断地为每个 entity 都创建一个
    FooFactory
    class,即使其
    create()
    并没有执行 constructor 无法完成的工作。
  • “FactoryFactory”间接层——几乎总是代码库过度设计的迹象。
  • 会返回中间状态的 builder——调用方可能持有一个仅构建了一半的
    Order
    并调用其方法。

See also

另请参阅

  • ddd-entity
    — what the factory typically creates.
  • ddd-aggregate
    — factories produce whole aggregates, not half-aggregates.
  • ddd-repository
    — the boundary; factory creates, repository persists.
  • ddd-entity
    ——factory 通常创建的对象。
  • ddd-aggregate
    ——factory 生成完整的 aggregate,而不是处于半构建状态的 aggregate。
  • ddd-repository
    ——两者之间的边界;factory 负责创建,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)。