Successfully added MediatR to the Food Storage API to implement the Command Query Responsibility Segregation (CQRS) pattern following clean architecture principles.
- Added MediatR 14.0.0 to:
FoodStorageApi.ApplicationprojectFoodStorageApi.Apiproject
- Updated
ApplicationModule.csto register MediatR services:IMediatorinterface registration- Automatic handler registration from Application assembly
- Support for both
IRequestHandler<TRequest, TResponse>andIRequestHandler<TRequest>
Created organized folder structure following clean architecture:
src/Application/Features/
└── OpenFoodFacts/
└── Queries/
├── GetProductByBarcodeQuery.cs
├── GetProductByBarcodeQueryHandler.cs
├── SearchProductsQuery.cs
└── SearchProductsQueryHandler.cs
- Purpose: Retrieve product information by barcode
- Input:
string Barcode - Output:
OpenFoodFactsApiResponse? - Handler: Includes logging, validation, and error handling
- Purpose: Search products by name with pagination
- Input:
string ProductName, int Page = 1, int PageSize = 20 - Output:
IEnumerable<OpenFoodFactsProduct> - Handler: Includes logging, validation, and error handling
- Modified
FoodResearchControllerto use MediatR instead of direct service injection - Replaced
IOpenFoodFactsServicedependency withIMediator - Updated method implementations to send queries through MediatR
- Separation of Concerns: Business logic moved to Application layer handlers
- Dependency Direction: Controllers depend on abstractions, not concrete implementations
- Single Responsibility: Each handler focuses on one specific operation
- Query Separation: Distinct query objects for different operations
- Handler Isolation: Each query has its dedicated handler
- Scalability: Easy to add new queries/commands without modifying existing code
- Unit Testing: Handlers can be tested independently
- Mocking: Easy to mock
IMediatorin controller tests - Extensibility: Simple to add new features through additional queries/commands
- Request Pipeline: MediatR provides pipeline for cross-cutting concerns
- Async Support: Full async/await pattern support
- Memory Efficiency: Request/response pattern minimizes object allocation
Easy to add command handlers for write operations:
CreateFoodItemCommandUpdateFoodItemCommandDeleteFoodItemCommand
Can implement cross-cutting concerns:
ValidationBehavior<TRequest, TResponse>LoggingBehavior<TRequest, TResponse>CachingBehavior<TRequest, TResponse>
Support for domain events:
INotificationHandler<TNotification>- Event-driven architecture patterns
- ✅ Solution builds successfully
- ✅ MediatR services registered correctly in DI container
- ✅ Controllers use MediatR instead of direct service calls
- ✅ Query handlers implement business logic with proper error handling
- ✅ Logging and validation maintained throughout the pipeline
- Consider adding pipeline behaviors for validation and logging
- Implement command handlers for write operations
- Add unit tests for new query handlers
- Consider adding caching behavior for frequently accessed data