Diving into Domain-Driven Design: Entities and Aggregates

By Levon Zhang

Domain-Driven Design (DDD) introduces several key concepts that are vital for understanding and implementing business logic in software development. Among these, Entities and Aggregates, particularly Root Entities, play a crucial role.

Understanding Entities in DDD

Entities are objects that are defined not just by their attributes, but by a thread of continuity and identity. Unlike Value Objects, Entities are distinguished by their identity rather than their attributes.

Key Characteristics of an Entity

  • Identity: Entities have a unique identifier.
  • Mutable: Their attributes can change over time while the identity remains constant.

Root Entity and its Role in an Aggregate

A Root Entity, often referred to as the Aggregate Root, is the main entity within an Aggregate. An Aggregate is a cluster of domain objects that can be treated as a single unit for data changes.

Functions of a Root Entity

  • Ensures Rules: The Root Entity enforces all invariants for the Aggregate.
  • Entry Point: It acts as the sole entry point for any modifications to the Aggregate.

Example: E-Commerce Customers and Orders

In an e-commerce system, we can consider ‘Order’ as an Aggregate Root. Here, ‘Customer’ is an Entity, and ‘Order’ is the Root Entity of the Order Aggregate.

PlantUML Class Diagram

Entity image

Implementation in Python

class Customer:
    def __init__(self, customer_id, name):
        self.customer_id = customer_id
        self.name = name

class Order:
    def __init__(self, order_id, customer):
        self.order_id = order_id
        self.customer = customer
        self.order_items = []

    def add_order_item(self, order_item):
        self.order_items.append(order_item)

class OrderItem:
    def __init__(self, product_id, quantity, price):
        self.product_id = product_id
        self.quantity = quantity
        self.price = price

Example Usage

customer = Customer("C123", "John Doe")
order = Order("O456", customer)
order_item = OrderItem("P789", 2, 19.99)
order.add_order_item(order_item)

In this scenario, ‘Order’ is the Aggregate Root that controls and ensures the integrity of ‘OrderItem’ objects. The ‘Customer’ entity is related to ‘Order’, but it is not part of the same Aggregate.

By understanding and applying these concepts, developers can create more robust and business-aligned software systems.


Entities and Aggregates form the backbone of Domain-Driven Design, enabling developers to model real-world business scenarios effectively.

Share: Twitter Facebook LinkedIn