Skip to content

Entities

Why Entities?

Not every concept in a domain stands on its own. An OrderItem only makes sense within an Order; a Reservation belongs to a Booking. These objects have their own identity, because you need to distinguish one line item from another. But they don't control their own lifecycle, because their parent creates, changes, and deletes them. If you modeled every identified object as an independent aggregate, you'd lose the ability to enforce rules that span the parent and its children (e.g. "an order's total must equal the sum of its line items").

Entities are the answer: objects with unique, stable identity that live inside an aggregate. The aggregate root controls their creation, mutation, and deletion, ensuring that cross-object invariants are always satisfied.

Entities are similar to Aggregates in nature and purpose. In fact, an Aggregate is nothing but an entity chosen to represent the entire cluster.

Entities are mutable and encapsulate both state and behavior. They have distinct identity. This identity remains consistent throughout the life of the entity, regardless of changes to its attributes.

Facts

Entities have an identity.

The primary characteristic of an entity is its unique identity, which distinguishes it from other objects. This identity is independent of the entity's attributes and remains constant throughout the entity's lifecycle.

Entities are mutable.

Entities can change state over time while maintaining their identity. This mutability is essential for modeling real-world objects that undergo various state changes.

Entities can contain other entities or value objects.

Entities can compose other entities and value objects, enabling the modeling of complex domain relationships. However, keep the composed structure manageable. Keep every composed object inside the boundaries of its aggregate.

Entities enforce business rules.

Entities are responsible for ensuring that business rules and invariants are upheld within their scope. They encapsulate the logic necessary to maintain domain integrity.

Entities in one domain can be value objects in another.

Entities in one domain can be a Value Object in another.

For example, a seat is an Entity in the Theatre domain if the theatre allocate seat numbers to patrons. If visitors are not allotted specific seats, then a seat can be considered a ValueObject, as one seat can be exchanged with another.

Aggregates handle entity persistence.

Entities are persisted and retrieved through their aggregates. You should not query or update entities directly, because that skips the aggregate's invariant checks. The entity can then end up in an invalid state.

Associations define how entities are connected. The aggregate typically controls these associations, so it can keep them consistent and intact.

Entities can contain nested entities and value objects.

Entities can nest other entities and value objects to form hierarchical structures. The aggregate controls these nested objects as part of its lifecycle. Their values form part of the overall state of the entity.

Entity graphs should be kept manageable.

While entities can form complex graphs, it is crucial to maintain manageable structures to avoid excessive complexity. To ensure performance and manageability, entities should be part of aggregates that are appropriately sized. If an aggregate becomes too large, consider splitting it into smaller, more manageable aggregates.


Next steps

For practical details on defining and using entities in Protean, see the guide:

  • Entities: Defining entities, configuration options, and associations within aggregates.

Not sure whether your concept should be an entity, aggregate, or value object?

For design guidance: