Skip to main content

Posts

Showing posts with the label Domain-Driven Design

Transactional Consistency in Hexagonal Architecture: The Unit of Work Pattern

  The hardest boundary to maintain in Hexagonal Architecture (Ports and Adapters) is the one between your Application layer and your Persistence layer when   ACID transactions   are involved. You have likely faced this dilemma: The "Dirty" Approach:  You pass a database transaction object (like JPA’s  EntityManager  or a Prisma  tx  client) into your Domain services.  Result:  Your domain is now coupled to your database library. The "Magic" Approach:  You rely on framework decorators like  @Transactional  (Spring/NestJS).  Result:  This often works for simple CRUD, but fails when composing complex Use Cases where the transaction boundary needs to be explicit, or when you are strictly separating your core logic into libraries that do not depend on the framework. In a strict Clean Architecture, the Use Case (Application Service) dictates the transaction boundary, but the Domain layer must remain ignorant of  h...

DDD Aggregates: Solving the N+1 Performance Trap in Large Hierarchies

  It is the quintessential DDD hangover. You spend weeks modeling your domain with experts. You identify a   Distributor   entity that holds a collection of   Orders , which in turn hold   LineItems . It feels semantically correct: an Order cannot exist without a Distributor. You write the code, the tests pass, and you deploy. Three months later, the system hangs whenever a Distributor with 5,000 historical orders tries to open their dashboard. You have fallen into the  Mega-Aggregate Trap . By strictly adhering to the rule that "Aggregates are consistency boundaries," you have inadvertently forced your ORM (Entity Framework Core or Hibernate) to hydrate an entire object graph just to change a user's email address. This post details the root cause of this performance collapse and provides a rigorous architectural pattern to decouple large hierarchies without sacrificing data integrity. The Root Cause: Consistency vs. Transactional Reality The performance is...