Overview
The Intent.Application.MediatR module implements the CQRS (Command Query Responsibility Segregation) design pattern using the MediatR library. It generates commands, queries, and their corresponding handlers based on your service models.This module is part of the CQRS paradigm. If you prefer a traditional service-based approach, consider using the
Intent.Application.ServiceImplementations module instead.What Gets Generated
When you model commands and queries in the Services Designer, this module generates:- Command Classes -
IRequest<TResponse>implementations representing write operations - Command Handlers -
IRequestHandler<TRequest, TResponse>implementations processing commands - Query Classes -
IRequest<TResponse>implementations representing read operations - Query Handlers -
IRequestHandler<TRequest, TResponse>implementations processing queries - ICommand Interface - Marker interface for identifying commands
- IQuery Interface - Marker interface for identifying queries
CQRS Pattern Benefits
The CQRS paradigm provides:- Separation of Concerns - Read and write operations are optimized independently
- Use Case-Centric - Each operation is a discrete request/response object
- Pipeline Behaviors - Add validation, logging, transactions as cross-cutting concerns
- Loose Coupling - Mediator pattern decouples callers from handlers
- Scalability - Read and write models can scale independently
When to Use CQRS
- Operations benefit from specialized pipeline behaviors
- Strict separation between commands and queries
- Individual operations have complex concerns
- Loose coupling via mediator pattern
When to Use Traditional Services
- Unified domain model and service structure
- Operations grouped by business capability
- Class-based approach with methods
- Team familiar with traditional patterns
Generated Code Examples
Command Example
Given a “Create Customer” command modeled in the Services Designer:CreateCustomerCommand.cs
Command Handler Example
CreateCustomerCommandHandler.cs
Query Example
GetCustomerByIdQuery.cs
Query Handler Example
GetCustomerByIdQueryHandler.cs
Configuration
Consolidate Command/Query Associated Files
When enabled, commands/queries and their handlers are generated in a single file instead of separate files in sub-folders.Disabled (default):Enabled:
Property Default Values
Command and query properties can have default values in the Services Designer. Default values are applied in the constructor only if no subsequent properties lack default values. Incorrect ordering (default value ignored):MediatR License Options
Starting with MediatR v13.0, a commercial license is required. Prior versions remain free.Controls which version of MediatR is used:
- Enabled: Locks to the last free version (pre-v13)
- Disabled: Uses the latest commercial edition (requires license key)
Configuring Your License Key
If using the commercial version, configure your license key: Option 1: appsettings.jsonGet a License Key
Learn how to obtain a MediatR commercial license
Dependencies
This module requires:Intent.Application.DependencyInjection.MediatR- Registers MediatR servicesIntent.Common.CSharp- C# code generation utilitiesIntent.Modelers.Services.CQRS- CQRS modeling designer
Related Modules
MediatR Behaviours
Add cross-cutting concerns like logging, validation, and transactions
MediatR CRUD
Auto-generate CRUD operation implementations
MediatR FluentValidation
Add FluentValidation to your command/query pipeline
Additional Resources
- MediatR GitHub Repository
- CQRS Pattern Overview
- Module ID:
Intent.Application.MediatR
