Design, refactor, analyze, and review code by applying the principles and patterns of tactical domain-driven design. Triggers on: domain modeling, aggregate design, 'entity', 'value object', 'repository', 'bounded context', 'domain event', 'domain service', code touching domain/ directories, rich domain model discussions.
version
1.0.0
Tactical DDD
Design, refactor, analyze, and review code by applying the principles and patterns of tactical domain-driven design.
Principles
Isolate domain logic
Use rich domain language
Orchestrate with use cases
Avoid anemic domain model
Separate generic concepts
Make the implicit explicit... like your life depends on it
Design aggregates around invariants
Extract immutable value objects liberally
Repositories are for loading and saving full aggregates
1. Isolate domain logic
What: Domain logic is not mixed with technical code like HTTP and database transactions.
Why: Easier to understand the most important part of the code, easier to validate with domain experts, easier to test and evolve, easier to plan and implement new features.
Test: Could a domain expert read the code? Can the code be unit tested without mocks or spinning up databases?
What: Names in code match exactly what domain experts say. No programmer jargon. No generic names.
Why: Translation between code-speak and business-speak causes bugs. When a domain expert says "assess a claim" and the code says "processEntity", someone will misunderstand something.
Test: Would a domain expert recognize this name? If you'd need to translate it for them, it's wrong.
Common generic terms to watch for:
Manager, Handler, Processor, Helper, Util
Data, Info, Item (when domain terms exist)
process, handle, execute (what does it actually DO?)
What: A use case is a user goal—something a user would recognize as an action they can perform in your application.
Why: Use cases define the entry points to your domain. They answer "what can a user do?" If something isn't a user goal, it's supporting machinery that belongs elsewhere.
Test (the menu test): If you described your application's features to a user like a menu, would this be on it?
DELIVERY APP MENU:
├── Request Delivery ← Use case: user goal
├── Track Delivery ← Use case: user goal
├── Cancel Delivery ← Use case: user goal
├── Calculate ETA ← NOT a use case: internal machinery
└── Check Delivery Radius ← NOT a use case: domain rule
// ❌ WRONG - not a user goal, this is internal machinery// use-cases/calculate-eta.use-case.tsasyncfunctioncalculateETA(deliveryId: DeliveryId) {
const delivery = await deliveryRepository.find(deliveryId)
const driver = await driverRepository.find(delivery.driverId)
return routeService.estimateArrival(driver.location, delivery.destination)
}
// ✅ RIGHT - actual user goal (appears in menu)// use-cases/cancel-delivery.use-case.tsasyncfunctioncancelDelivery(deliveryId: DeliveryId, reason: CancellationReason) {
const delivery = await deliveryRepository.find(deliveryId)
delivery.cancel(reason)
await deliveryRepository.save(delivery)
}
4. Avoid anemic domain model
What: Domain logic lives in domain objects, not in use cases. Use cases orchestrate; domain objects decide.
Why: When business rules leak into use cases, they scatter across the codebase, duplicate, and diverge. The domain becomes a dumb data carrier.
Test: Is your use case making business decisions, or just coordinating? If the use case contains if/else business logic, you likely have an anemic model.
// ❌ WRONG - business logic in use case (anemic domain)asyncfunctionconfirmDropoff(deliveryId: DeliveryId, photo: ProofPhoto) {
const delivery = await deliveryRepository.find(deliveryId)
// Business rules leaked into use case!if (delivery.status !== 'in_transit') {
thrownewError('Delivery not in transit')
}
if (!photo && delivery.requiresSignature) {
thrownewError('Proof of delivery required')
}
delivery.status = 'delivered'
delivery.proofPhoto = photo
delivery.deliveredAt = newDate()
await deliveryRepository.save(delivery)
}
// ✅ RIGHT - use case orchestrates, domain decidesasyncfunctionconfirmDropoff(deliveryId: DeliveryId, photo: ProofPhoto) {
const delivery = await deliveryRepository.find(deliveryId)
delivery.confirmDropoff(photo) // Domain enforces the rulesawait deliveryRepository.save(delivery)
}
Signs of anemic model:
Use cases full of if/else business logic
Domain objects are just data with getters/setters
Business rules duplicated across multiple use cases
Validation logic outside the object being validated
5. Separate generic concepts
What: Generic capabilities that aren't specific to your domain live separately from domain-specific logic.
Why: A retry mechanism, a caching layer, a validation framework—these aren't YOUR domain. Mixing them with domain logic obscures what's actually specific to your business.
Test: Would this code exist in a completely different business domain? If yes, it's generic. If it's specific to YOUR business rules, it's domain.
// ❌ WRONG - generic retry logic mixed with domain// domain/driver-locator.tsclassDriverLocator {
// Generic retry logic does not belong in domain!privateasync withRetry<T>(fn: () =>Promise<T>, attempts: number): Promise<T> {
for (let i = 0; i < attempts; i++) {
try { returnawaitfn() }
catch (e) { if (i === attempts - 1) throw e }
}
thrownewError('Retry failed')
}
asyncfindAvailableDriver(zone: Zone): Promise<Driver> {
returnthis.withRetry(() =>this.searchDriversInZone(zone), 3)
}
privateasyncsearchDriversInZone(zone: Zone): Promise<Driver> {
// domain logic to find nearest available driver
}
}
// ✅ RIGHT - same behavior, properly separated// infra/retry.ts (generic, reusable in any project)exportasyncfunction withRetry<T>(fn: () =>Promise<T>, attempts: number): Promise<T> {
for (let i = 0; i < attempts; i++) {
try { returnawaitfn() }
catch (e) { if (i === attempts - 1) throw e }
}
thrownewError('Retry failed')
}
// domain/driver-locator.ts (pure domain, no infra imports)classDriverLocator {
asyncfindAvailableDriver(zone: Zone): Promise<Driver> {
// domain logic to find nearest available driver
}
}
// use-cases/dispatch-delivery.ts (orchestrates domain + infra)asyncfunctiondispatchDelivery(deliveryId: DeliveryId) {
const delivery = await deliveryRepository.find(deliveryId)
const driver = awaitwithRetry(
() => driverLocator.findAvailableDriver(delivery.zone), 3
)
delivery.assignDriver(driver)
await deliveryRepository.save(delivery)
}
6. Make the implicit explicit... like your life depends on it
What: Strive for maximum expressiveness. Go as far as possible to identify and name domain concepts in code. Don't settle for "good enough"—push until the code speaks the domain fluently.
Why: Maximum alignment optimizes communication between engineers and domain experts. Easier to discuss nuances and avoid misconceptions. Easier to plan and implement features and detect when the design of code is causing unnecessary friction.
Test: Could you discuss this code with a domain expert without translation? Are there concepts they use that don't exist in your code?
// This code looks fine - isolated, uses domain termsclassDelivery {
status: DeliveryStatusdriver: Driver | nullpickupTime: Date | nulldropoffTime: Date | nullproofOfDelivery: Photo | nullassignDriver(driver: Driver): void {
if (this.status !== DeliveryStatus.Confirmed) thrownewError('...')
this.driver = driver
this.status = DeliveryStatus.Assigned
}
recordPickup(): void {
if (this.status !== DeliveryStatus.Assigned) thrownewError('...')
this.pickupTime = newDate()
this.status = DeliveryStatus.InTransit
}
recordDropoff(photo: Photo): void {
if (this.status !== DeliveryStatus.InTransit) thrownewError('...')
this.proofOfDelivery = photo
this.dropoffTime = newDate()
this.status = DeliveryStatus.Delivered
}
}
// But the TYPES can describe the domain! Each state is a distinct concept.// Reading the types alone tells you how deliveries work.typeDelivery =
| RequestedDelivery// Customer placed request
| ConfirmedDelivery// Restaurant accepted
| AssignedDelivery// Driver assigned, heading to restaurant
| InTransitDelivery// Driver picked up, heading to customer
| DeliveredDelivery// Complete with proofinterfaceRequestedDelivery {
kind: 'requested'customer: Customerrestaurant: Restaurantitems: MenuItem[]
}
interfaceConfirmedDelivery {
kind: 'confirmed'customer: Customerrestaurant: Restaurantitems: MenuItem[]
estimatedPrepTime: Duration
}
interfaceAssignedDelivery {
kind: 'assigned'customer: Customerrestaurant: Restaurantitems: MenuItem[]
driver: Driver// Now guaranteed to existestimatedPickup: Time
}
interfaceInTransitDelivery {
kind: 'in_transit'customer: Customerrestaurant: Restaurantitems: MenuItem[]
driver: DriverpickupTime: Time// Now guaranteed to existestimatedDropoff: Time
}
interfaceDeliveredDelivery {
kind: 'delivered'customer: Customerrestaurant: Restaurantitems: MenuItem[]
driver: DriverpickupTime: TimedropoffTime: Time// Now guaranteed to existproofOfDelivery: Photo// Now guaranteed to exist
}
// State transitions are explicit functionsfunctionconfirmDelivery(d: RequestedDelivery, prepTime: Duration): ConfirmedDeliveryfunctionassignDriver(d: ConfirmedDelivery, driver: Driver): AssignedDeliveryfunctionrecordPickup(d: AssignedDelivery): InTransitDeliveryfunctionrecordDropoff(d: InTransitDelivery, photo: Photo): DeliveredDelivery
Smaller improvements matter too:
// Extract an if statement to a named methodif (distance.kilometers > 10 && !driver.hasLongRangeVehicle) { ... }
if (delivery.exceedsDriverRange(driver)) { ... }
// Name a boolean expressionconst canAssign = driver.isAvailable && driver.isInZone(delivery.zone) && !driver.atCapacityconst canAssign = driver.canAccept(delivery)
// Rename to use domain languageconst fee = customFee ?? standardFee
const fee = customFee ?? defaultDeliveryFee
Ways to increase expressiveness:
Model states as distinct types (Delivery with status → RequestedDelivery, ConfirmedDelivery, etc.)
Make optional fields guaranteed at the right state (driver: Driver | null → driver: Driver)
Extract conditionals to named methods (complex if → exceedsDriverRange)
Rename variables to use domain language (standardFee → defaultDeliveryFee)
7. Design aggregates around invariants
What: An aggregate is a cluster of objects that must be consistent together. The aggregate root enforces the rules. External code cannot violate invariants.
Why: Without clear boundaries, inconsistent states creep in. One piece of code updates the delivery, another updates the route, and suddenly the ETA is wrong.
Test: What must be true at all times? What rules must never be broken? The objects involved in those rules form an aggregate.
Methods should operate on the same state—if they don't, split the aggregate
8. Extract immutable value objects liberally
What: When something is defined by its attributes (not identity), make it an immutable value object. Do this liberally—more value objects is usually better.
Why: Value objects are simple. They can't change unexpectedly. They're easy to test. They make domain concepts explicit. They're also a good way to extract logic from aggregates and entities that can easily get large—keep entities focused by pulling cohesive concepts into value objects.
Test: Does this need a unique ID to track it over time? No? It's probably a value object.
// Entity with primitives that should be a value objectclassDelivery {
id: DeliveryIdfeeAmount: numberfeeCurrency: string
}
// Extract the value objectclassDelivery {
id: DeliveryIdfee: Money
}
classMoney {
constructor(readonlyamount: number,
readonlycurrency: Currency) {}
add(other: Money): Money {
if (this.currency !== other.currency) {
thrownewCurrencyMismatchError(this.currency, other.currency)
}
returnnewMoney(this.amount + other.amount, this.currency)
}
equals(other: Money): boolean {
returnthis.amount === other.amount && this.currency === other.currency
}
}
Good candidates for value objects:
Money, Currency, Percentage
DateRange, TimeSlot, Duration
Address, Coordinates, Distance
EmailAddress, PhoneNumber, URL
Quantity, Weight, Temperature
PersonName, CompanyName
9. Repositories are for loading and saving full aggregates
The job of a repository is to load and save entire aggregates - not partial aggregates or nested entities inside an aggregate. The load method takes an ID and returns the full aggregate.
A repository should not exist for a domain object that is not an aggregate. Entity that is part of an aggreate -> does not have a repository. It is loaded via the aggregate root's repository.
The hydrate method is used ONLY for constructing an aggregate from it's persisted state. It should not be abused for other use cases like creating new instances. Each creation flow should have a dedicated factory method, e.g. Order.fromExisting(), Order.new(), Order.draft().
The save method of a repository should take the full aggregate.
If you just want to query information to display without modifying state and applying business rules, create a separate read model object and don't use a repository.
Mandatory Checklist
When designing, refactoring, analyzing, or reviewing code:
Verify domain is isolated from infrastructure (no DB/HTTP/logging in domain; generic utilities in infra; domain doesn't import infra)
Verify names are from YOUR domain, not generic developer jargon
Verify use cases are intentions of users, human or automated (apply the menu test)
Verify business logic lives in domain objects, use cases only orchestrate
Verify states are modeled as distinct types where appropriate
Verify hidden domain concepts are extracted and named explicitly
Verify aggregates are designed around invariants, not naive mapping of domain nouns
Verify values are extracted into value objects expressing a domain concept
Veirfy no abuse of hydrate methods for creation scenarios. Each creation scenario must have dedicated factory method