Factory
A factory groups complex or business-significant creation without taking over later state changes.
- InputsCustomer, line items and address.
- FactoryChecks preconditions and creates.
- AggregateStarts in a valid state.
Context
Why this is important
Create valid domain objects with explicit construction rules.
Creation of new entities or aggregates and validation of their initial conditions.
Factory and direct construction
The order factory creates the aggregate; the repository stores it. Both belong to the Ordering module.
This is how you read the picture
- Inputs Order data
- Factory Construction
- Aggregate valid start
| Criterion | Situation | Suitable form |
|---|---|---|
| Simple values | Constructor | Few rules |
| Aggregates with rules | Factory | Checked preconditions |
Benefits & limitations
Helps if:
- Construction groups several domain preconditions or assembles an aggregate from several valid parts.
- Callers should receive a fully valid object without needing to know internal construction steps or the concrete implementation.
Boundaries
- For an object with a few clear required values, a constructor remains easier to understand and maintain.
- A factory establishes the valid initial state; persistence, retrieval, and later changes belong to other components.
In practice
- Identify the preconditions a new object must satisfy and express them as a business creation rule rather than a collection of technical setters.
- Use a factory when an aggregate requires several objects or callers should not know its concrete implementation. A constructor is enough for simple, unambiguous creation.
- Return a fully valid object and test invalid input, boundaries and each business precondition. Keep storage access and later state changes outside the creation contract.
Read the detailed explanation
A factory can create one object or a composed aggregate. Its work ends after valid construction; persistence and later state changes belong to other components.
A factory is useful when preconditions or composition need explanation. A simple constructor is preferable when it ensures the same validity directly and clearly.
The factory creates the Aggregate in a valid initial state; a Repository handles storage afterward.
Check your understanding
Test yourself in three stages
ReadingCheck in the picture
Which inputs does the order factory use?
View the solution with justification
Customer, line items and confirmed delivery address.
ApplyingTransferred to a new situation
A simple immutable value has no preconditions. What construction is enough?
View the solution with justification
A direct constructor.
EvaluateWeighing an alternative
When does a factory provide clear domain value?
View the solution with justification
When it groups several initial invariants or gives complex creation a clear business name.
Explore further
These building blocks continue the thoughts of this page.
References
- Eric Evans · DDD Reference (opens in new tab)Primary reference for repository and factory patterns.
- DDD Reference · German translation (opens in new tab)German edition of Eric Evans’ reference.
- Vaughn Vernon · Implementing Domain-Driven Design (opens in new tab)Further treatment of tactical DDD building blocks and examples.