Wednesday, July 23, 2025

What is Domain-Driven Design (DDD)?

DDD is a software design approach focused on modeling software based on the real-world domain it is intended to serve, using a ubiquitous language and breaking systems into bounded contexts.


💡 Key Principles of DDD

PrincipleDescription
Ubiquitous LanguageShared vocabulary used by developers and domain experts
Bounded ContextA logical boundary around a domain model (e.g., Order, Payment)
EntitiesObjects defined by identity and lifecycle (e.g., Customer, Invoice)
Value ObjectsImmutable objects defined by attributes, not identity (e.g., Address)
AggregatesCluster of domain objects with one root (Aggregate Root)
RepositoriesProvide access to aggregates (e.g., CustomerRepository)
Domain EventsRepresent something that happened in the domain (OrderPlaced)
ServicesDomain logic that doesn’t naturally belong to an entity or value object
FactoriesEncapsulate object creation logic

🧱 Building Blocks of DDD (Example in Java)

// Entity
public class Customer { private UUID id; private String name; private Address address; // value object } // Value Object public class Address { private String street; private String city; } // Aggregate Root public class Order { private UUID orderId; private List<OrderItem> items; public void addItem(OrderItem item) { ... } }

🔀 Bounded Contexts & Integration

Each bounded context represents a specific part of the domain with its own model. They integrate using:

MethodDescription
REST APIsSynchronous communication
Events (EDA)Asynchronous via Kafka/RabbitMQ
Shared KernelShared code in tightly coupled domains

📌 Real-World DDD Scenario (E-commerce)

Bounded ContextDescription
Order ContextHandles order creation and status
Inventory ContextManages stock and warehouse data
Payment ContextDeals with payment and refunds

“Each context has its own model, entities, services, and database. They interact via REST or Kafka events using a common ubiquitous language.”


🎯 Why Use DDD?

“We used DDD to model a complex domain with multiple teams working in parallel. It helped us isolate domain logic into bounded contexts, enabling clear ownership, better code organization, and easier scaling.”

✅ Benefits:

  • Aligns code with business

  • Improves maintainability

  • Scales well across teams

  • Enhances testability

❌ When Not to Use:

  • Simple CRUD apps or reporting systems

  • Domains not well understood


🛠️ Spring Boot DDD Best Practices

PracticeTool/Pattern
Package by domaincom.company.order, com.company.payment
Use interfaces for repositoriesJpaRepository<Order, UUID>
Handle events via listeners@DomainEvent, @TransactionalEventListener
Avoid anemic modelsPut behavior inside entities
Use @Service, not fat controllersKeeps domain logic clean

📘 Sample Package Structure

com.example.order
├── domain │ ├── model │ ├── service │ ├── repository │ └── event ├── application ├── infrastructure └── api

🧠 Interview Answer Template

“We adopted DDD to model our logistics and finance systems separately. We defined clear bounded contexts and used domain events to enable async communication. For example, the OrderPlaced event in the Order context triggers inventory reservation in the Inventory context. This allowed us to scale teams independently and maintain high cohesion within each context.”

No comments:

Post a Comment