Identity Stability

In most cases unique identity must be protected from modification, remaining stable throughout the lifetime of the Entity to which it is assigned.

Trivial measures may be taken to prevent identity modification. We can hide identity setters from clients. We might also create guards in setters to prevent even the Entity itself from changing the state of the identity if it already exists. Guards are coded as assertions in Entity setters. Here’s an example of an identity setter:

public class User extends Entity  {     ...     protected void setUsername(String aUsername) {         if (this.username != null) {             throw new IllegalStateException(                     "The username may not be ...

Get Implementing Domain-Driven Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.