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:

  1. Order Aggregate:
    • Root Entity: Order
    • Associated Entities: OrderLine, Customer, ShippingAddress, Payment
    • In this example, an Order serves as the root of the aggregate. It encapsulates various entities such as OrderLine representing individual items in the order, Customer providing information about the customer placing the order, ShippingAddress specifying where the order should be shipped, and Payment handling 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
}
  1. Blog Post Aggregate:
    • Root Entity: BlogPost
    • Associated Entities: Comment, Author
    • A BlogPost aggregate might consist of the main post (BlogPost entity), comments left by readers (Comment entities), and information about the author (Author entity). The BlogPost entity 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
}
  1. Library Book Aggregate:
    • Root Entity: Book
    • Associated Entities: Author, Publisher, Review
    • In a library context, a Book aggregate could include information about the book itself, such as title and ISBN (International Standard Book Number), the Author entity representing the person who wrote the book, Publisher providing details about the publishing house, and Review containing 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
}
  1. E-commerce Product Aggregate:
    • Root Entity: Product
    • Associated Entities: ProductVariant, Price, Inventory
    • An Product aggregate might include information about the product itself, such as name and description, along with related entities like ProductVariant representing different versions of the product, Price specifying the cost, and Inventory tracking 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
}
  1. Project Management Task Aggregate:
    • Root Entity: Task
    • Associated Entities: Assignee, Subtask, DueDate
    • In a project management context, a Task aggregate could involve the main task (Task entity), the person assigned to the task (Assignee entity), any subtasks associated with the main task (Subtask entities), and the due date (DueDate entity).
// 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.

domain-driven-design-ddd
aggregate
examples