Skip to content

Latest commit

 

History

History
122 lines (82 loc) · 3.76 KB

File metadata and controls

122 lines (82 loc) · 3.76 KB

MediatR Integration for Clean Architecture

Overview

Successfully added MediatR to the Food Storage API to implement the Command Query Responsibility Segregation (CQRS) pattern following clean architecture principles.

Changes Made

1. Package Installation

  • Added MediatR 14.0.0 to:
    • FoodStorageApi.Application project
    • FoodStorageApi.Api project

2. AutoFac Configuration

  • Updated ApplicationModule.cs to register MediatR services:
    • IMediator interface registration
    • Automatic handler registration from Application assembly
    • Support for both IRequestHandler<TRequest, TResponse> and IRequestHandler<TRequest>

3. Application Layer Structure

Created organized folder structure following clean architecture:

src/Application/Features/
└── OpenFoodFacts/
    └── Queries/
        ├── GetProductByBarcodeQuery.cs
        ├── GetProductByBarcodeQueryHandler.cs
        ├── SearchProductsQuery.cs
        └── SearchProductsQueryHandler.cs

4. Query Implementation

GetProductByBarcodeQuery

  • Purpose: Retrieve product information by barcode
  • Input: string Barcode
  • Output: OpenFoodFactsApiResponse?
  • Handler: Includes logging, validation, and error handling

SearchProductsQuery

  • 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

5. Controller Updates

  • Modified FoodResearchController to use MediatR instead of direct service injection
  • Replaced IOpenFoodFactsService dependency with IMediator
  • Updated method implementations to send queries through MediatR

Benefits Achieved

Clean Architecture Compliance

  • 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

CQRS Implementation

  • 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

Testing & Maintainability

  • Unit Testing: Handlers can be tested independently
  • Mocking: Easy to mock IMediator in controller tests
  • Extensibility: Simple to add new features through additional queries/commands

Performance Benefits

  • Request Pipeline: MediatR provides pipeline for cross-cutting concerns
  • Async Support: Full async/await pattern support
  • Memory Efficiency: Request/response pattern minimizes object allocation

Future Enhancements

Commands

Easy to add command handlers for write operations:

  • CreateFoodItemCommand
  • UpdateFoodItemCommand
  • DeleteFoodItemCommand

Pipeline Behaviors

Can implement cross-cutting concerns:

  • ValidationBehavior<TRequest, TResponse>
  • LoggingBehavior<TRequest, TResponse>
  • CachingBehavior<TRequest, TResponse>

Event Handling

Support for domain events:

  • INotificationHandler<TNotification>
  • Event-driven architecture patterns

Verification

  • ✅ 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

Next Steps

  1. Consider adding pipeline behaviors for validation and logging
  2. Implement command handlers for write operations
  3. Add unit tests for new query handlers
  4. Consider adding caching behavior for frequently accessed data