SOLID & Design Patterns Interview Question #72

What is an Aggregate Root?

Advanced Patterns, CQRS & Domain-Driven Design Fundamentals Mid-Level Intermediate

Quick Interview Answer

An Aggregate Root is the entity that acts as the public entry point to an aggregate. Other objects outside the aggregate should normally reference and modify the aggregate through the root rather than changing internal entities directly.

Detailed Explanation

An Aggregate Root is the entity that acts as the public entry point to an aggregate. Other objects outside the aggregate should normally reference and modify the aggregate through the root rather than changing internal entities directly.

This protects business invariants and makes the transaction boundary clearer. Repository interfaces in DDD are commonly defined around aggregate roots rather than every database table.

Code Example

// Order is the aggregate root.
var order = await repository.GetByIdAsync(orderId);

order.AddItem(product, 2);
order.Confirm();

await unitOfWork.SaveChangesAsync();