---
topic: dev-practices
author: Crashtech Editorial
published: Nov 13, 2026 · read: 3 min
---

Entities, Value Objects, Aggregates: DDD's Building Blocks via Their Hardest Questions

How do you compare two entities? Two value objects? What does an aggregate protect? DDD's building blocks, explained by the question each one answers.

– –

DDD vocabulary scares people off because it reads like a glossary. It is easier to learn from the questions each term answers. Start with two that trip people up in code review: how do you check whether two entities are equal, and how do you check two value objects?

The two equality rules

Entity equal by identity
A Customer with ID 42 is the same customer after changing their name, address and email. Equality compares the ID, not the attributes. Entities have a lifecycle: created, changed, perhaps archived.
Value object equal by attributes
Ten US dollars is ten US dollars, whichever instance you hold. No identity, usually immutable. Equality compares every attribute, and “changing” one means creating a new one.

That single distinction removes a lot of bugs. Money as a bare number loses its currency. An email as a plain string accepts garbage. A Money or EmailAddress value object validates once at construction and never lies afterwards.

Aggregates: the consistency boundary

An aggregate is a cluster of entities and value objects treated as one unit for changes, with one entity as the aggregate root. The rules are simple and strict:

  1. Outside code holds references only to the root.
  2. Changes to anything inside go through the root, so it can enforce invariants.
  3. The aggregate is the unit of loading, saving and transactional consistency.

An Order root with its OrderLine entities is the classic example. The invariant “an order’s total equals the sum of its lines, and it cannot exceed the credit limit” lives on the root. Nobody edits a line directly and breaks it.

Keep aggregates small

The usual mistake is a giant aggregate that loads half the database. Design around invariants that must be true together in one transaction, and reference other aggregates by ID, not by object, so each stays small and cheap to load.

Advertisement

The supporting cast

Building blockJob
RepositoryStores and loads aggregates, hiding persistence behind a collection-like interface
FactoryCreates aggregates in a valid state when construction is complex
Domain serviceDomain logic that does not belong on one entity or value object, often spanning aggregates
Domain eventA record of something that happened to an aggregate, published so others can react

Domain events are how aggregates stay decoupled: OrderPlaced is published, and billing, inventory and email each react on their own, without the order aggregate knowing they exist.

A small example

class Money {                        // value object
  constructor(readonly amount: number, readonly currency: string) {}
  equals(o: Money) { return this.amount === o.amount && this.currency === o.currency; }
  add(o: Money) { /* checks same currency, returns a new Money */ }
}

class Order {                         // aggregate root (an entity)
  constructor(readonly id: OrderId, private lines: OrderLine[] = []) {}
  addLine(product: ProductId, qty: number, price: Money) {
    if (qty <= 0) throw new Error("quantity must be positive");  // invariant
    this.lines.push(new OrderLine(product, qty, price));
  }
  total(): Money { /* sum lines */ }
}

Do

Model concepts defined by their attributes as value objects, put invariants on the aggregate root, and reference other aggregates by ID.

Don't

Expose an aggregate’s internals with public setters, or make everything an entity. You lose the guarantees that made the model worth building.

Takeaway

DDD’s building blocks are answers to specific design questions. Does it have identity? Entity. Is it defined by its values? Value object. Which objects must stay consistent together? Aggregate, behind one root. Ask the question first and the vocabulary stops being a glossary and starts being a design tool.

Advertisement

Frequently asked questions

How do you check equality of two entities versus two value objects?

Two entities are equal if they have the same identity, even if every attribute differs, because identity persists across change. Two value objects are equal if all their attributes are equal, because they have no identity. Two money objects of 10 USD are interchangeable. Two customers named the same are not.

What is an aggregate root and why does it matter?

An aggregate is a cluster of related objects treated as one unit for data changes, with one entity, the root, as the only entry point. Outside code references the root only, and the root enforces invariants across the whole cluster. This is the unit you load, save and keep consistent in a single transaction.

When should a domain concept be a value object?

When it is defined entirely by its attributes, has no lifecycle of its own, and can be treated as immutable and replaceable: money, date ranges, addresses, coordinates. Making these value objects removes primitive obsession and puts validation in one place.

What is the difference between a domain service and a repository?

A repository stores and retrieves aggregates, abstracting persistence behind a collection-like interface. A domain service holds domain behaviour that does not naturally belong on one entity or value object, often coordinating several aggregates. Repositories are about access, domain services are about logic.

Sources & further reading

/* Comments */