What is domain-driven design, and what are its core principles?
Domain-Driven Design (DDD), introduced by Eric Evans in his 2003 book, is an approach to building software for complex business domains by putting a rich model of the business at the centre of the code and developing it together with domain experts. Its core principles are: focus on the core domain (where the business competes), a ubiquitous language shared by experts and developers and used in the code, bounded contexts that give each model a clear boundary, and tactical building blocks (entities, value objects, aggregates, domain events, repositories, domain services) that keep business rules explicit and protected. DDD pays off when business logic is complex; for simple CRUD applications it is usually overkill.
Read full answerHide full answer
Applies toenterprise systems with complex rules (ordering, banking, insurance, logistics, healthcare), modular monoliths and microservice architectures
Strategic Design (the Big Picture)
| Principle | Meaning |
|---|---|
| Core, supporting and generic subdomains | Invest the most modelling effort in the core domain; buy or simplify generic ones (email, auth) |
| Ubiquitous language | One precise vocabulary per context, used in conversations, documentation and code |
| Bounded contexts | Each model applies within an explicit boundary; “Order” in Sales may differ from “Order” in Shipping |
| Context maps | Describe how contexts relate (customer-supplier, shared kernel, anti-corruption layer) |
Tactical Design (Inside a Context)
| Building block | Role | Order example |
|---|---|---|
| Entity | Identity that persists over time | Order with order_id |
| Value object | Defined by its values; immutable | Money(amount, currency), Address |
| Aggregate | Consistency boundary with one root | Order (root) containing OrderLines |
| Domain event | Something important that happened | OrderPlaced |
| Repository | Loads and saves whole aggregates | OrderRepository |
| Domain service | Domain logic that does not belong to one entity | PricingService |
| Factory | Creates complex aggregates in a valid state | Order.create(...) |
Small Example
from dataclasses import dataclass, field
from decimal import Decimal
@dataclass(frozen=True)
class Money: # value object
amount: Decimal
currency: str
def __add__(self, other):
assert self.currency == other.currency
return Money(self.amount + other.amount, self.currency)
@dataclass
class OrderLine:
product_id: str
quantity: int
unit_price: Money
@dataclass
class Order: # aggregate root (entity)
order_id: str
customer_id: str # other aggregate referenced by ID
lines: list = field(default_factory=list)
status: str = "draft"
events: list = field(default_factory=list)
def add_line(self, product_id, quantity, unit_price):
if self.status != "draft":
raise ValueError("cannot change a placed order")
if quantity <= 0:
raise ValueError("quantity must be positive")
self.lines.append(OrderLine(product_id, quantity, unit_price))
def total(self):
total = Money(Decimal("0"), "INR")
for line in self.lines:
total = total + Money(line.unit_price.amount * line.quantity, line.unit_price.currency)
return total
def place(self):
if not self.lines:
raise ValueError("an order needs at least one line")
self.status = "placed"
self.events.append(("OrderPlaced", self.order_id, self.total().amount))
order = Order("o-1", "c-42")
order.add_line("p-1", 2, Money(Decimal("499"), "INR"))
order.place()
print(order.status, order.total().amount, order.events[0][0]) # placed 998 OrderPlaced
The rules (“cannot change a placed order”, “needs at least one line”) live in the model, named in the business’s own terms, not scattered across controllers and SQL.
Rules of Thumb
-
Change one aggregate per transaction, and reference other aggregates by ID (the order holds
customer_id, not aCustomerobject). -
Keep aggregates small; use domain events for consistency across aggregates.
-
Let business language drive names:
place(), notset_status(2).
When DDD Fits and When It Does Not
| Fits | Overkill |
|---|---|
| Complex, changing business rules | Simple CRUD admin screens |
| Access to domain experts | Data pipelines with little logic |
| Long-lived core systems | Short-lived prototypes |
| Several teams and models | Small apps with one obvious model |
Interview Tip
Define DDD as modelling a complex domain with experts, then cover strategic design (subdomains, ubiquitous language, bounded contexts, context maps) and tactical design (entities, value objects, aggregates, events, repositories). Stating the one-aggregate-per-transaction rule and when DDD is overkill shows balanced judgement.