13: Factories

Chapter Roadmap

  • Learn why the use of Factories can produce expressive models that adhere to Ubiquitous Language

  • See how factory methods can be used as Aggregate Behaviors

  • Consider how to use Factory Methods to create Aggregate instances of their types

  • Learn how Domain Services can be designed as Factories while interacting with other Bounded Contexts and translating foreign objects to local types.


Factories in the Domain Model

  • Shift the responsibility of creating instances of complex objects and Aggregates to a separate object.

  • Even if these objects have no responsibility in the domain model, they are still part of the domain model.

  • Factories provide an interface that encapsulates complex assembly without requiring the client to reference concrete classes of objects instantiated.

  • Factories can create an entire Aggregates as a piece while enforcing their invariants.

  • An aggregate root that provides a Factory for another Aggregate type will have the primary responsibility of providing main aggregate behavior, the Factory being just one of those.

  • Factory Methods on aggregates allow you to express the Ubiquitous Language in way not possible through constructors.

  • When the behavioral name is expressive with respect to the Ubiquitous Language, we have a powerful case for using a Factory Method.

Last updated