Clean Architecture
Infrastructure Interface Adapters: Controllers, Presenters, Gateways
"infr"/[entity]/repo"
- Repository: Persist domain entity to datastore
- Description: Repositories are Facades to persistence technologies (such as ORMs).
A Facade is defined by an object that provide a simplified interface to a larger body of code.
Repos persist and retrieve domain entities.
- Persistence objectives
- Scaffold complex persistence logic across junction and relationship tables.
- Rollback transactions that fail
- On save(), check if the entity already exists and then perform the create or update.
- Retrieval objectives
- Retrieve the entirety of data needed to create domain entities
- Delegate the responsibility of reconsituting entities to a Mapper.
- Retrieval objectives
- Persistence objectives
- Scaffold complex persistence logic across junction and relationship tables.
- Rollback transactions that fail
- On save(), check if the entity already exists and then perform the create or update.
"infr/[entity]/ctrl"
- Controller: Handles API Requests
"infr/[entity]/interpreter"
- Interpreter: Maps one object implementation to a DTO (Custom Pattern)
"infr/[entity]/mapper"
- Mapper: Transforms Data
- Description:
- From Domain to DTO
- From Domain to Persistence
- From Persistence to Domain
"infr/[entity]/dto"
- Data Transfer Object: An object that carries data between two separate systems
- Description:
- In web terms this is a View Model. They aren't the real domain model, but they contain the data the view needs to know about
- Can be used on the front-end and back-end to define the API entry contract
Use Cases Application Business Rules: Use Cases
"use-cases/[entity]/[use-case]"
Domain Enterprise Business Rules: Entities
"domain/[entity]/validator"
- performing validation on the domain object
"domain/[entity]/index"
- Entity: Enforce model invariants
- Description:
- Aggregate - a cluster of associated objects treated as a unit for the purpose of data changes
- Description:
Answers the following questions:
- How do we (cascade and) save this cluster of entities to the database?
Does the following:
- Entities and value objects are clustered together into aggregates
- An "aggregate" is a cluster of associated objects that we treat as a unit for the purpose of data changes."
- The boundary is how far fully constituted entities are in the aggregate.
- With all fully constituted entities inside of the aggregate, it's possible for the aggregate root to enforce all invariants within the aggregate when it's state changes.
- An aggregate must always be returned fully constituted from persistence. That constraint requires us to think hard about performance constraints and what's really necessary to fully pull from the database.
- Our aggregate design goals are to:
- Provide enough info to enforce model invariants within a boundary
- Execute use cases
- Ensure decent database performance
- Provide enough info to transform a Domain Entity to a DTO (optional)
- How do we (cascade and) save this cluster of entities to the database?
Does the following:
Defintions:
- A subdomain is a logical separation of the entire problem domain.
References:
How to Design & Persist Aggregates - Domain-Driven Design w/ TypeScript
Better Software Design with Application Layer Use Cases | Enterprise Node.js + TypeScript