Skip to main content

Posts

Showing posts with the label NestJS

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...

Preventing 'Anemic Domain Models' in Hexagonal Architecture with NestJS

  You adopted Hexagonal Architecture (Ports and Adapters) and Domain-Driven Design (DDD) to tame complexity. Yet, looking at your NestJS codebase, the "Domain" layer often looks suspiciously empty. You likely have   User   or   Order   classes that are nothing more than bags of properties decorated with TypeORM or MikroORM annotations, entirely void of behavior. This is the  Anemic Domain Model  anti-pattern. Even in a modular architecture, if your Service layer holds 100% of the  if/else  logic and validation, and your Entities are just data carriers, you are writing procedural code disguised as Object-Oriented Programming. You aren't doing DDD; you're doing "Transaction Script" with extra steps. The Root Cause: Why NestJS Pushes You Toward Anemia The NestJS ecosystem, while excellent, inadvertently encourages anemic models through two primary vectors: ORM Coupling:  Tutorials often teach you to use  @Entity()  classes as your...