How royib/clean-architecture-nestjs works
Project’s Nature and Purpose
• This project is a backend API built with NestJS and TypeScript that follows the principles of Clean Architecture. Its purpose is to structure the business logic (use cases and entities) independently from the implementation details (such as database access and external CRM services).
File Structure and Map to Architectural Layers
• Core Domain and Business Logic:
• Use Cases:
• Use cases for handling business rules are in “src/use-cases” (further partitioned by domain – author, book, genre). This layer orchestrates how the core business rules are applied to specific functions.
• Controllers:
• The “src/controllers” folder contains the entry points for HTTP requests. These represent the “gateways” into the use cases.
• Services:
• “src/services” holds service modules for handling external operations, grouped further into CRM services and data services. They act as intermediaries that connect controllers and use-case logic with the underlying implementations.
• Frameworks (Implementation Details):
• The “src/frameworks” directory leverages specific implementations for external concerns such as data persistence and CRM integrations:
• Under “src/frameworks/data-services/mongo”, you have the models, data services modules, and repository implementations for MongoDB.
• Under “src/frameworks/crm-services/salesforce”, you have the specific modules and service implementations for integrating with Salesforce.
• Configuration and Bootstrapping:
• Files like “src/configuration” and “src/main.ts” provide setup and bootstrap details. While these aren’t core to the layered design, they support the overall infrastructure.
Main Components
• Inner Core:
• Entities (business objects) – represent the domain model.
• DTOs and Abstracts – define contracts and data structures used by the use cases.
• Business Use Cases:
• Represented by the “use-cases” modules, each with specific logic for author, book, and genre functionalities.
• Interface/Delivery Mechanisms:
• Controllers – serve as the entry points translating HTTP (or other) requests into calls to use cases.
• External Interfaces:
• Frameworks – including:
• Data Services (Mongo) for persistence.
• CRM Services (Salesforce) for external integrations.
• Service Layer:
• Contains additional orchestration logic that can be presented as intermediaries between use cases and frameworks.
Relationships and Data Flows
• Controllers call use cases.
• Use cases invoke the domain entities and leverage abstractions defined in the core.
• Use cases indirectly access external systems and persistence layers through the service layer and framework implementations.
• That the innermost layers (entities and use cases) are independent of the outer layers (frameworks).
Important Architectural Patterns and Design Principles
• Clean Architecture:
• That any external detail (database, third-party CRM) is isolated in the outer layers.
• SOLID Principles:
• The abstract classes and DTOs support a design where concrete implementations can be swapped without impacting core logic.
• Modular Separation:
• Each domain (author, book, genre) has its own use-case folder with corresponding factories, ensuring separation concerning business functionalities.