14 Nov 2023
Intermediate
In Domain-Driven Design (DDD), the term "Aggregate" refers to a cluster of associated domain objects that are treated as a single unit. Aggregates are defined by a root entity, which acts as the entry point to the aggregate and ensures the consistency and integrity of the entire cluster. Here are a few examples to illustrate the concept of Aggregate in DDD:
- Order Aggregate:
- Root Entity: Order
- Associated Entities: OrderLine, Customer, ShippingAddress, Payment
- In this example, an
Orderserves as the root of the aggregate. It encapsulates various entities such asOrderLinerepresenting individual items in the order,Customerproviding information about the customer placing the order,ShippingAddressspecifying where the order should be shipped, andPaymenthandling the payment details.
// Aggregate Root
public class Order {
private List<OrderLine> orderLines;
private Customer customer;
private ShippingAddress shippingAddress;
private Payment payment;
// Methods and business logic related to order
// Ensure consistency and integrity within the aggregate
}
// Associated Entity 1
public class OrderLine {
private Product product;
private int quantity;
// Methods related to order line
}
// Associated Entity 2
public class Customer {
private String name;
private String email;
// Methods related to customer
}
// Associated Entity 3
public class ShippingAddress {
private String street;
private String city;
private String zipCode;
// Methods related to shipping address
}
// Associated Entity 4
public class Payment {
private BigDecimal amount;
private PaymentStatus status;
// Methods related to payment
}
- Blog Post Aggregate:
- Root Entity: BlogPost
- Associated Entities: Comment, Author
- A
BlogPostaggregate might consist of the main post (BlogPostentity), comments left by readers (Commententities), and information about the author (Authorentity). TheBlogPostentity acts as the root, ensuring that comments and author information are maintained and updated consistently.
// Aggregate Root
public class BlogPost {
private String title;
private String content;
private List<Comment> comments;
private Author author;
// Methods and business logic related to blog post
// Ensure consistency and integrity within the aggregate
}
// Associated Entity 1
public class Comment {
private String text;
private Author commenter;
// Methods related to comments
}
// Associated Entity 2
public class Author {
private String name;
private String bio;
// Methods related to author
}
- Library Book Aggregate:
- Root Entity: Book
- Associated Entities: Author, Publisher, Review
- In a library context, a
Bookaggregate could include information about the book itself, such as title and ISBN (International Standard Book Number), theAuthorentity representing the person who wrote the book,Publisherproviding details about the publishing house, andReviewcontaining reader reviews.
// Aggregate Root
public class Book {
private String title;
private String ISBN;
private Author author;
private Publisher publisher;
private List<Review> reviews;
// Methods and business logic related to books
// Ensure consistency and integrity within the aggregate
}
// Associated Entity 1
public class Author {
private String name;
private String bio;
// Methods related to author
}
// Associated Entity 2
public class Publisher {
private String name;
private String address;
// Methods related to publisher
}
// Associated Entity 3
public class Review {
private String text;
private int rating;
// Methods related to reviews
}
- E-commerce Product Aggregate:
- Root Entity: Product
- Associated Entities: ProductVariant, Price, Inventory
- An
Productaggregate might include information about the product itself, such as name and description, along with related entities likeProductVariantrepresenting different versions of the product,Pricespecifying the cost, andInventorytracking the stock levels.
// Aggregate Root
public class Product {
private String name;
private String description;
private List<ProductVariant> variants;
private Price price;
private Inventory inventory;
// Methods and business logic related to products
// Ensure consistency and integrity within the aggregate
}
// Associated Entity 1
public class ProductVariant {
private String variantName;
private String SKU;
// Methods related to product variants
}
// Associated Entity 2
public class Price {
private BigDecimal amount;
private Currency currency;
// Methods related to pricing
}
// Associated Entity 3
public class Inventory {
private int stockQuantity;
// Methods related to inventory
}
- Project Management Task Aggregate:
- Root Entity: Task
- Associated Entities: Assignee, Subtask, DueDate
- In a project management context, a
Taskaggregate could involve the main task (Taskentity), the person assigned to the task (Assigneeentity), any subtasks associated with the main task (Subtaskentities), and the due date (DueDateentity).
// Aggregate Root
public class Task {
private String title;
private String description;
private Assignee assignee;
private List<Subtask> subtasks;
private DueDate dueDate;
// Methods and business logic related to tasks
// Ensure consistency and integrity within the aggregate
}
// Associated Entity 1
public class Assignee {
private String name;
private String role;
// Methods related to assignee
}
// Associated Entity 2
public class Subtask {
private String title;
private String description;
// Methods related to subtasks
}
// Associated Entity 3
public class DueDate {
private LocalDateTime dueDateTime;
// Methods related to due dates
}
In each of these examples, the aggregate is a collection of related entities, with one entity designated as the root. The root entity ensures that the consistency rules within the aggregate are maintained, and changes to the aggregate are made through the root entity to preserve integrity.