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.

ITPUB
ITPUB
ITPUB
How to Design DDD Code Implementation Models: A Practical Guide with Real-World Example

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.

Domain object model diagram
Domain object model diagram

Application Layer

Application services reside in an application package, split into commandservice and queryservice. They depend on domain objects and orchestrate commands and queries.

Application service model diagram
Application service model diagram

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

Infrastructure model diagram
Infrastructure model diagram

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.
Context integration inbound/outbound diagram
Context integration inbound/outbound diagram

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.

Ticket context code model
Ticket context code model
Ticket subpackage diagram
Ticket subpackage diagram
// 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend ArchitectureMicroservicesDomain-Driven DesignSpring BootDDDcode-structure
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.