# Entity Life Cycle

**In short:** An entity life cycle is the path an entity follows over time, from the moment it gets an identity until it ends. Named, guarded transitions and invariants stop it from entering invalid states.

## Explanation

An entity is a domain object with an identity that persists through time and across different representations (see the article Entity). The life cycle describes what happens to that entity along the way: which states it can be in and how it moves between them.

**Transitions are named and guarded.** Each change of state should be a named method, such as "ship" or "cancel". Each method carries a guard. A guard is a check that rejects an invalid change. The method raises an error the moment code tries a forbidden move.

**Invariants protect the end of the life cycle.** An invariant is a rule that must always hold. One useful invariant blocks any change to an entity in a terminal state, meaning a final state such as a completed or cancelled order. The aggregate therefore enforces its own life cycle.

**Aggregates carry the rules.** An aggregate is a cluster of domain objects treated as one unit (see the article Aggregate). Its root entity ensures the integrity of the whole cluster. Owned objects are reached only through that root, and the root is responsible for them. So the root is the natural place to keep life-cycle rules.

**Factories and repositories cover the ends.** The start of the life cycle is creation. A factory handles complex construction of an object or aggregate and keeps creation logic out of the model. A repository separates application and domain logic from persistence. In most cases only aggregate roots need to be searchable through it.

## Example

Consider an order in a web shop. A factory builds it from a basket and gives it an identity. The order then moves through states such as placed, paid, shipped, and cancelled.

The order has a method "ship". Its guard rejects the call if the order is not yet paid, and raises an error at once. Once the order is cancelled, a rule on the order root blocks any further change. Other code reaches the order's line items only through the order itself.

## Trade-offs

Putting guards and invariants inside the aggregate keeps the rules in one place and catches mistakes immediately. The cost is that every allowed transition must be designed and named up front. The facts here do not cover further trade-offs.

Generic order life cycle: guarded transitions between states, ending in terminal states.

```mermaid
stateDiagram-v2
    [*] --> Placed: create via factory
    Placed --> Paid: pay
    Paid --> Shipped: ship
    Placed --> Cancelled: cancel
    Paid --> Cancelled: cancel
    Shipped --> [*]
    Cancelled --> [*]
```

## Checks

- State changes are named methods that reject invalid transitions immediately.
- Invariants block changes to entities in terminal states.
- The aggregate root enforces integrity, and owned objects are reached only through it.
- Complex creation of objects or aggregates is handled by factories, not by the model itself.

## Related

- [Entity](https://arc.fuet.ch/concepts/domain-driven-design/entity.md)
- [Aggregate](https://arc.fuet.ch/concepts/domain-driven-design/aggregate.md)
- [State Machine Diagram](https://arc.fuet.ch/concepts/modeling-uml/state-machine-diagram.md)
- [Business Object Lifecycle](https://arc.fuet.ch/concepts/modeling-bpmn/business-object-lifecycle.md)
- [Domain Model](https://arc.fuet.ch/concepts/domain-driven-design/domain-model.md)
- [Entity and Value Object](https://arc.fuet.ch/concepts/domain-driven-design/entity-vs-value-object.md)
- [Bounded Context](https://arc.fuet.ch/concepts/domain-driven-design/bounded-context.md)
- [DDD Building Blocks (Tactical Patterns)](https://arc.fuet.ch/concepts/domain-driven-design/ddd-modeling-elements.md)

## Sources

- [Evans Classification](https://martinfowler.com/bliki/EvansClassification.html) — martinfowler.com, accessed 2026-11-09
- [Aggregate State Machines - Protean](https://docs.proteanhq.com/patterns/aggregate-state-machines/) — docs.proteanhq.com, accessed 2026-11-09
- [D D D_ Aggregate](https://martinfowler.com/bliki/DDD_Aggregate.html) — martinfowler.com, accessed 2026-11-09
- [DDD.6 – The lifecycle of a domain object – @hgraca](https://herbertograca.com/2015/10/04/domain-driven-design-by-eric-evans-chap-6-the-lifecycle-of-a-domain-object/) — herbertograca.com, accessed 2026-11-09
