DDD Tactical Patterns: Entities, Value Objects, Aggregates
Model rich domain rules using Entities, Value Objects, Aggregates, Repositories, and Domain Events on the shared Order service.
TL;DR
Tactical DDD puts business invariants inside the domain model instead of anemic service scripts: Value Objects (Money) are immutable, Entities (Order) have identity, Aggregates guard consistency boundaries, Repositories persist whole aggregates, and Domain Events (OrderPlaced) announce state changes.
Intent
Enforce business invariants in one place inside an Aggregate Root (like Order) so invalid state—such as an empty order or a negative total—cannot be constructed anywhere in the codebase.
Problem
In an "anemic domain model", Order is just a plain data bag (interface Order { status: string; items: any[] }) and validation rules scatter across controllers, cron jobs, and services. One endpoint forgets to check items.length > 0 or recalculates totalCents differently, corrupting data in production.
Solution
Tactical DDD gives you five building blocks that fit naturally inside Hexagonal or Clean Architecture:
- Value Object (
Money): immutable, validated on creation (amountCents >= 0), compared by value. - Entity (
Order,OrderLine): has a uniqueidand mutable lifecycle (draft→placed→cancelled). - Aggregate Root (
Order): ownsOrderLine[]and exposes intent-revealing methods (order.place()). No outside code mutatesorder.linesdirectly. - Repository (
OrderRepository): saves and loads the entireOrderaggregate as one atomic unit. - Domain Event (
OrderPlaced): immutable record emitted by the aggregate when a meaningful business fact occurs.
Real-world analogy
▶ Flow: watch it work
Command → Aggregate Invariant → Domain Event
Press Start or tap any step dot to trace the path.
▸All steps (6)
Code
The shared Order example modeled as a Value Object (Money), Aggregate Root (Order), and Domain Event (OrderPlaced):
// TypeScript 7.0.2
export class Money {
private constructor(
public readonly cents: number,
public readonly currency: 'USD' | 'EUR',
) {}
static of(cents: number, currency: 'USD' | 'EUR' = 'USD'): Money {
if (!Number.isInteger(cents) || cents < 0) throw new Error('Invalid Money');
return new Money(cents, currency);
}
add(other: Money): Money {
if (other.currency !== this.currency) throw new Error('Currency mismatch');
return Money.of(this.cents + other.cents, this.currency);
}
}
export interface OrderPlaced {
readonly type: 'OrderPlaced';
readonly orderId: string;
readonly totalCents: number;
}
export class Order {
private events: OrderPlaced[] = [];
private constructor(
public readonly id: string,
private readonly items: ReadonlyArray<{ sku: string; price: Money; qty: number }>,
private status: 'draft' | 'placed',
) {}
static create(id: string, items: Array<{ sku: string; price: Money; qty: number }>): Order {
if (items.length === 0) throw new Error('Order must have at least one item');
return new Order(id, items, 'draft');
}
place(): OrderPlaced[] {
if (this.status !== 'draft') throw new Error('Order already placed');
this.status = 'placed';
const total = this.items.reduce(
(sum, item) => sum.add(Money.of(item.price.cents * item.qty, item.price.currency)),
Money.of(0),
);
this.events.push({ type: 'OrderPlaced', orderId: this.id, totalCents: total.cents });
return [...this.events];
}
}When to use / when to avoid
- Your domain has real state-transition rules and invariants (orders, payments, inventory reservations, bookings).
- You already use Hexagonal or Clean Architecture and want a rich domain core instead of anemic DTOs.
- Simple CRUD forms or admin tables with no invariants—tactical DDD adds ceremony for zero payoff.
- Reporting/analytics queries—bypass Aggregates and read directly with SQL.
How to implement
Extract Value Objects first
Replace raw number or string primitives (cents, email, currency) with self-validating Value Objects.
Keep Aggregates small
Include only the child entities needed to enforce an immediate invariant; reference other Aggregates by ID only.
Emit Domain Events on state transitions
Record events inside the Aggregate Root and publish them after the repository commits.
Pros / Cons
- Invariants are impossible to bypass because constructors and methods guard every state transition.
- Pure TypeScript domain classes test in microseconds with zero mocks.
- Requires mapping between rich Aggregates and relational/SQL tables in the Repository.
- Oversized Aggregates cause lock contention when multiple users edit unrelated child entities.
Relations
Check yourself
Check Yourself
1. Why should an Order Aggregate hold customerId: string instead of a full Customer object reference?
2. Which DDD building block is immutable, has no ID, and is compared by its property values?
Sources
Related Notes
DDD Essentials: Ubiquitous Language, Bounded Context, Context Map
Align code and domain experts using Ubiquitous Language, isolate models inside Bounded Contexts, and document integration boundaries with a Context Map.
CQRS: Command Query Responsibility Segregation
Separate write (command) models from read (query) models when read and write shapes diverge—and know when plain CRUD is better.