How pratiksinghlad/cqrspatternapi works
Project Type and Purpose
• Project Type: This is a CQRS (Command Query Responsibility Segregation) example built on .NET 8.
• Purpose: The application demonstrates how to separate command (write) operations from query (read) operations. It uses the Mediator pattern (via MediatR) and utilizes separate database contexts for write and read operations, following an Onion Layer architecture to enforce dependency inversion.
File Structure
• Main Components:
• CQRSPattern.Api: The web API hosting project. It contains controllers (like EmployeesController) and startup configurations (multiple Startup.*.cs files for health checks, mediator, scalar, etc.).
• CQRSPattern.Application: The core business logic layer with commands, queries, validators, and domain models (e.g., Employee commands for add, update, and queries for get all).
• CQRSPattern.Infrastructure.Persistence: Handles database-related concerns including separate DbContexts (WriteDbContext, ReadDbContext), entity configurations, migrations, and repository implementations both for read and write.
• CQRSPattern.Infrastructure.Mediator: Encapsulates MediatR configurations, mediator factories, and custom pipeline behaviors (e.g., validation behaviors).
• CQRSPattern.Migrator: A dedicated project for handling migrations and possibly setting up the database (with its own Program, HostedService, and connection factory).
• Test Projects (Application Test, Infrastructure Persistence Test, and Shared Test): They provide unit test coverage for the application and persistence layers.
Key Architectural Elements from the README
• CQRS Pattern: Emphasizes separation between command operations (modifying data, e.g., AddEmployee, UpdateEmployee) and query operations (reading data, e.g., GetAllQuery).
• Onion/Layers: The architecture respects dependency directions from controllers to the application core, and from inner layers to infrastructure.
• Mediator Pattern: The Application layer uses the MediatR library to decouple the controllers from business logic.
• Database Separation: Two distinct database contexts (read and write) are used, improving scalability and potential security.
• External Tools/Frameworks: Uses Entity Framework Core for ORM, OpenAPI for API documentation, and Scalar (an alternative/testing tool for Swagger) for API calling and testing.
a. Main Components to Include:
• API Layer (CQRSPattern.Api):
• Contains endpoints like the Employee Controller.
• Handles incoming HTTP requests, applies routing, and passes responsibilities via MediatR.
• Implements health checks and configuration settings.
• Application Layer (CQRSPattern.Application):
• Includes business logic with Commands (e.g., AddEmployeeCommand, UpdateEmployeeCommand) and Queries (e.g., GetAllQuery).
• Defines validators and command handlers.
• Acts as the entry point for MediatR which decouples direct calls between UI and business logic.
• Infrastructure Layer:
• Persistence: Contains separate DbContexts (ReadDbContext for queries, WriteDbContext for commands), entity configurations, repositories (IEmployeeReadRepository, IEmployeeWriteRepository), and migrations.
• Mediator: Contains the mediator implementations, custom behaviors (e.g., validation), and related utilities.
• External Integration: Any health checks, environment configuration files (appsettings.json variants), and database connection managers (e.g., MySqlConnectionManager).
• Migrator:
• A supporting service responsible for applying migrations and database setup.
b. Relationships and Interactions:
• API layer receives HTTP requests and routes them to the appropriate Controller actions.
• Controllers pass commands or queries to the Mediator (MediatR) in the Application layer.
• The Application layer processes requests via appropriate handlers:
• For commands (create, update), the flow goes to repository methods in the Infrastructure Persistence layer using WriteDbContext.
• For queries, it reaches the Infrastructure Persistence layer using ReadDbContext.
• The Mediator component ensures decoupled communication and centralized handling of requests.
• Configuration and startup files in the API layer integrate health checks, MediatR setup, and documentation (OpenAPI).
Architectural Patterns to Highlight
• CQRS Pattern: Separate flows for command (write) and query (read) operations.
• Onion/Layered Architecture: Strict dependency flow from the outer API layer to the inner Core/Application layer, then to Infrastructure.
• Mediator Pattern: Decoupling via MediatR, facilitating separation of concerns.
• Persistence Pattern: Using separate contexts to isolate read and write operations.