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.

    6.1 Managing state using entities

    6.2 Consistent on creation

    6.2.1 The perils of no-arg constructors

    6.2.2 ORM frameworks and no-arg constructors

    6.2.3 All mandatory fields as constructor arguments

    6.2.4 Construction with a fluent interface

    6.2.5 Catching advanced constraints in code

    6.2.6 The builder pattern for upholding advanced constraints

    6.2.7 ORM frameworks and advanced constraints

    6.2.8 Which construction to use when

    6.3 Integrity of entities

    6.3.1 Getter and setter methods

    6.3.2 Avoid sharing mutable objects

    6.3.3 Securing the integrity of collections

    Summary

    sitemap