Graduate Program KB

Domain Driven Design

Chapter 6 - The Lifecycle of a Domain Object

Aggregates

  • A cluster of related objects treated as a single unit
  • Each aggregate has a root and boundary
    • Root entity ensures consistency within the aggregate
    • Boundary defines what's inside the aggregate
  • Aggregates simplify complex object graphs by managing relationships, external references only point to the root, not the internal objects

Factories

  • A pattern for creating complex objects and aggregates
  • It encapsulates object creation logic to avoid cluttering domain modes
  • Factories handle the construction of objects with invariants or complex dependencies
  • Ensures newly created objects are always in a valid state
  • Often used for creating aggregates and keeping domain models focused on behaviour rather than creation

Repositories

  • Provides access to aggregates, acting like a collection interface
  • It encapsulates data access and provides simple methods for retrieving and persisting domain objects
  • Hides the details of data storage
  • Ensures transactional consistency across the aggregate when interacting with databases
  • It works with the aggregates and ensures objects retrieved from the repository are complete and valid

Designing objects for relational databases

  • Object-relational mapping (ORM) bridges the gap between domain models and relational databases
  • Design domain models with relational constraints in mind
  • Relational databases favour normalised data whereas domain models prefer encapsulation and aggregation
  • Mapping aggregates to tables may require careful design to maintaing consistency and performance