Skip to content

Instantly share code, notes, and snippets.

@gajewsk2
Last active July 22, 2021 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gajewsk2/db63e7612a162c198c6730969bd24d50 to your computer and use it in GitHub Desktop.
Save gajewsk2/db63e7612a162c198c6730969bd24d50 to your computer and use it in GitHub Desktop.
Clean Architecture README

Architecture

Clean Architecture

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.

"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)

Defintions:

  • A subdomain is a logical separation of the entire problem domain.

References:

Implementing DTOs, Mappers & the Repository Pattern using the Sequelize ORM [with Examples] - DDD w/ TypeScript

How to Design & Persist Aggregates - Domain-Driven Design w/ TypeScript

Better Software Design with Application Layer Use Cases | Enterprise Node.js + TypeScript

Clean Code TS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment