Backend Development 11 min read

Understanding Aggregate Roots and Domain Events in Domain-Driven Design with a Java Demo

This article explains the core DDD concepts of aggregate roots and domain events, illustrates them through a simplified e‑commerce scenario, and provides complete Java code examples for entities, value objects, events, and aggregate services to help developers apply DDD in backend systems.

JD Tech
JD Tech
JD Tech
Understanding Aggregate Roots and Domain Events in Domain-Driven Design with a Java Demo

Domain‑Driven Design (DDD) is a software development methodology that helps developers model complex business domains; two of its tactical patterns—aggregate roots and domain events—are essential for maintaining consistency and decoupling sub‑domains.

The article starts with a brief introduction to DDD and the motivation for using aggregate roots and domain events, then presents a minimal e‑commerce scenario (products, orders, order items, and addresses) to demonstrate these concepts.

It defines the core entities and value objects:

public class Product { private Long id; private String name; private BigDecimal price; private Integer stock; }

public class Order { private Long id; private LocalDateTime createTime; private Integer status; private List orderItems; }

public class OrderItem { private Long id; private Product product; private Integer quantity; private BigDecimal price; }

public class Address { private String province; private String city; private String district; private String detail; }

Domain events are modeled to capture important state changes:

public class OrderCreatedEvent { private Order order; private List orderItems; public OrderCreatedEvent(Order order, List orderItems) { ... } }

public class OrderPaidEvent { private Order order; private BigDecimal amount; public OrderPaidEvent(Order order, BigDecimal amount) { ... } }

public class OrderShippedEvent { private Order order; private String expressCompany; private String expressNo; public OrderShippedEvent(Order order, String expressCompany, String expressNo) { ... } }

Aggregate roots encapsulate business logic for their respective sub‑domains. The product aggregate provides CRUD operations via a ProductService :

public class ProductAggregate { private ProductService productService; public void createProduct(Product product) { productService.create(product); } public void updateProduct(Product product) { productService.update(product); } public void deleteProduct(Long productId) { productService.delete(productId); } public Product getProductById(Long productId) { return productService.getById(productId); } }

The order aggregate handles order creation, payment, and shipping while publishing the corresponding domain events:

public class OrderAggregate { private OrderService orderService; public void createOrder(Order order, List orderItems) { orderService.create(order); DomainEventPublisher.publish(new OrderCreatedEvent(order, orderItems)); } public void payOrder(Long orderId, BigDecimal amount) { orderService.pay(orderId, amount); DomainEventPublisher.publish(new OrderPaidEvent(orderService.getById(orderId), amount)); } public void shipOrder(Long orderId, String expressCompany, String expressNo) { orderService.ship(orderId, expressCompany, expressNo); DomainEventPublisher.publish(new OrderShippedEvent(orderService.getById(orderId), expressCompany, expressNo)); } public Order getOrderById(Long orderId) { return orderService.getById(orderId); } }

The article concludes that aggregate roots should contain rich behavior (not just data) and that domain events are powerful tools for decoupling, ensuring consistency, and enabling reactive workflows across bounded contexts.

backendJavaArchitectureDomain-Driven DesignAggregate RootDomain Event
JD Tech
Written by

JD Tech

Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.

0 followers
Reader feedback

How this landed with the community

login 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.