How to Design DDD Code Implementation Models: A Practical Guide with Real-World Example
This article explains how to translate complex Domain‑Driven Design concepts into concrete code structures, covering domain objects, application services, infrastructure, and context integration, and demonstrates the approach with a ticket‑service case study using Spring Boot and Spring Cloud Stream.
Designing the DDD Code Implementation Model
Domain Layer
All domain objects (aggregates, entities, value objects, domain events, repositories, command/query objects) are placed under a top‑level domain package.
Application Layer
Application services reside in an application package, split into commandservice and queryservice. They depend on domain objects and orchestrate commands and queries.
Infrastructure Layer
Technical concerns such as persistence, messaging, configuration and security are grouped under an infrastructure package. The exact sub‑packages vary per project.
Persistence
Messaging
Config
Security
Context Integration
Integration between bounded contexts is expressed with inbound (anti‑corruption layer, event handlers) and outbound (REST APIs, event publishers) packages.
Outbound publishes events; inbound consumes them to achieve loose coupling.
Case Study: Ticket, Staff and Order Bounded Contexts
The example implements a ticket‑processing scenario with three bounded contexts.
Ticket context – contains both inbound and outbound packages, uses TicketCommandService to call AclStaffService (REST ACL) and persist the Ticket aggregate via TicketRepository. Event handling is performed by OrderUpdatedEventHandler which consumes domain events from the Order context.
Staff context – only an outbound package exposing a REST API; persistence is handled by a simple infrastructure component.
Order context – publishes domain events through Spring Cloud Stream via OrderEventPublisherService; the Ticket context subscribes to these events.
// TicketCommandService uses ACL to call Staff and persist Ticket
public class TicketCommandService {
private final AclStaffService aclStaffService;
private final TicketRepository ticketRepository;
// ...
} // AclStaffService uses Spring RestTemplate for remote calls
@Service
public class AclStaffService {
private final RestTemplate restTemplate;
// ...
} // OrderEventPublisherService (Spring Cloud Stream)
@Service
public class OrderEventPublisherService {
private final StreamBridge streamBridge;
// ...
} // OrderUpdatedEventHandler in Ticket context consumes Order events
@Component
public class OrderUpdatedEventHandler {
@StreamListener("orderEvents")
public void handle(OrderUpdatedEvent event) {
// react to order changes
}
}All source code is available at https://github.com/tianminzheng/customer-service.
Conclusion
The presented package‑based DDD implementation model can be adapted to layered, clean or hexagonal architectures and to different technology stacks (e.g., Spring Boot, Axon). Mapping DDD concepts to concrete package structures clarifies dependencies and facilitates practical implementation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
