How kgrzybek/sample-dotnet-core-cqrs-api works
Main Components (Layers)
• Presentation/API Layer: Represented by the “SampleProject.API” project. This contains the controllers (e.g., CustomersController.cs, CustomerOrdersController.cs), middleware (like CorrelationMiddleware), configuration files, and the API bootstrap code (Program.cs, Startup.cs etc.).
• Application Layer: Shown in the “SampleProject.Application” directory. This layer organizes the CQRS components (commands, queries, handlers), integration handlers/notifications (such as OrderPlacedNotification, CustomerRegisteredNotificationHandler), validations, and processing modules. It glues the REST API to the domain logic.
• Domain Layer: Found in “SampleProject.Domain”. It contains core business entities such as Customer, Order, Payment, Product with their IDs, business rules, domain events, and shared kernel aspects like MoneyValue. This layer holds the business logic and DDD rules.
• Infrastructure Layer: Detailed in “SampleProject.Infrastructure”. This layer is responsible for data persistence (using EF Core), SQL connections, caching (MemoryCacheStore), email sending, logging, domain event dispatching, integration with Quartz.NET for the Outbox Pattern, and even seed work. It provides the technical implementation behind the abstractions defined in the Application and Domain layers.
• Database: Although only scripts (e.g., InitializeDatabase.sql) are provided, the database is a key external component used by raw SQL read models (via Dapper) and EF Core (write model).
Relationships and Interactions
• The API layer accepts REST calls which are then directed to controllers. Controllers invoke corresponding handlers in the Application layer.
• In the Application layer, incoming commands and queries are processed via MediatR and their respective handlers. Commands modify the state (write model) and trigger domain events, while queries retrieve data (read model) using raw SQL.
• The Domain layer enforces business rules, encapsulates business logic, and publishes domain events that may be handled asynchronously.
• The Infrastructure layer implements data access (repositories, EF Core DbContexts) and cross-cutting concerns like caching, logging, email notifications, and outbox processing.
• External flows include the “Cache-Aside” pattern (depicted as a caching component interacting with the Infrastructure layer) and an Outbox component (triggered by Quartz.NET jobs).
Architectural Patterns
• CQRS: Clearly separate the command (write) side from the query (read) side. Indicate that the read model is executed via Dapper and raw SQL, while the write side uses Domain Driven Design practices.
• DDD and Domain Events: Highlight domain entities, business rules, and event propagation.
• Integration Patterns: Include components for caching, problem details for API error handling, and the Outbox Pattern for integration and eventual consistency.
Specific Technologies and Frameworks
• .NET Core (as the framework for the API)
• MediatR (for command/query and domain event handling)
• Entity Framework Core (for write model persistence)
• Dapper (for executing raw SQL against database views for the read model)
• FluentValidation (for validating requests)
• Quartz.NET (for scheduling outbox process jobs)
• In-memory cache (for caching mechanisms)
Design Elements
• Use clear labels for each component (e.g., “API Controllers”, “Command Handlers”, “Domain Entities”, “Repositories”, “Cache”, “Outbox Processor”).
• From the API layer to the Application layer via HTTP requests.
• From the Application layer to the Domain layer when handling business logic.
• From the Domain layer to the Infrastructure layer for persistence and external integrations.
Final Recommendations
• Be as detailed as needed: make sure every critical component and dependency is accounted for. Include even auxiliary components like integration tests if beneficial for clarity.