What is Domain-Driven Design?
DDD is an approach to software development that focuses on understanding the business domain and modeling software around it. It's about language, boundaries, and patterns.
Core Concepts
Bounded Contexts
A bounded context defines the boundary within which a particular domain model applies. The same word can mean different things in different contexts.
// Order Context
class Order {
constructor(id, customerId, items, status) {
this.id = id;
this.customerId = customerId; // Reference, not full object
this.items = items;
this.status = status;
}
}
// Inventory Context
class StockItem {
constructor(productId, warehouse, quantity) {
this.productId = productId;
this.warehouse = warehouse;
this.quantity = quantity;
}
}Aggregates
An aggregate is a cluster of domain objects treated as a single unit for data changes. The aggregate root is the only entry point for modifications.
class OrderAggregate {
constructor(order) {
this.order = order;
this.items = [];
}
addItem(product, quantity) {
const item = new OrderItem(product.id, quantity, product.price);
this.items.push(item);
this.order.total = this.items.reduce((sum, i) => sum + i.subtotal, 0);
}
}Event Storming
A collaborative technique for discovering domain events, commands, and aggregates. Run a workshop with domain experts and developers together.
- Post sticky notes for domain events (orange)
- Identify commands that trigger events (blue)
- Group related events into aggregates (yellow)
- Define bounded contexts around clusters
Conclusion
DDD is not about patterns. It's about understanding the domain. Invest time in ubiquitous language and domain modeling before writing code.