6 Ensuring integrity of state
This chapter covers
- Managing mutable states using entities
- Ensuring an entity is consistent on creation
- Ensuring an entity keeps its integrity
Mutable state is an important aspect of systems. To some degree, changing state is the point of many systems, like the online bookstore of chapter 2. The system keeps track of a variety of state changes: books are put in a shopping cart, the order is paid for, and the books are shipped to the customer. If there aren’t state changes, not much interesting happens.1 Mutable state can be represented technically in many different ways. We’ll take a look at some alternatives and explore our preferred way—explicitly modeling mutable state in the style of Domain-Driven Design (DDD) as entities, as described in chapter 3.
1 Some conceptually interesting programming languages, such as Haskell, attempt to achieve this when programming with strictly immutable constructs. But most languages (Java, C, C#, Ruby, and Python, to name a few) do it using mutable constructs such as stateful objects.