Tactical DDD: Entities, Value Objects & Aggregates
Master the building blocks of Tactical Domain-Driven Design: Entities, Value Objects, Aggregate Roots, and Invariants.
While Strategic DDD helps you divide an enterprise into high-level Bounded Contexts, Tactical DDD provides the code-level design patterns to build rich, maintainable business logic inside a single microservice.
Without Tactical DDD, developers often fall into the Anemic Domain Model trap: creating dumb data classes with only getters/setters (@Getter @Setter) and scattering critical business validations across dozens of service classes.
Let's explore the core tactical building blocks.
01Entities vs Value Objects
The most fundamental distinction in domain modeling is between Entities and Value Objects:
| Characteristic | Entity | Value Object |
|---|---|---|
| Identity | Defined by a persistent, unique identifier (id) | Defined entirely by its attributes (has NO id) |
| Mutability | State can change over time while identity remains identical | Strictly Immutable |
| Equality | Evaluated by ID comparison (a.getId().equals(b.getId())) | Evaluated by structural equality of all fields |
| Examples | Customer, Order, BankAccount | Money(amount, currency), Address, Email |
Java 17+ Record as a Value Object:
package com.veltoryx.domain.model;
import java.math.BigDecimal;
import java.util.Objects;
// Value Object: Immutable, self-validating, compared by value
public record Money(BigDecimal amount, String currency) {
public Money {
Objects.requireNonNull(amount, "Amount cannot be null");
Objects.requireNonNull(currency, "Currency cannot be null");
if (amount.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Amount cannot be negative");
}
}
public Money add(Money other) {
if (!this.currency.equals(other.currency)) {
throw new IllegalArgumentException("Cannot add different currencies: " + currency + " and " + other.currency);
}
return new Money(this.amount.add(other.amount), this.currency);
}
}Notice that adding money returns a new instance of Money rather than mutating state.
Loading the rest of the lesson…