Thursday 2 June 2022

Clean Code Architecture with Mediator & CQRS pattern in .Net Core

 May be for some of you, The Mediator pattern is known but here my focus is to using the Mediator pattern with CQRS (Command and Query Responsibility Segregation) pattern to make our .net core (API or web) code cleaner, extensible and maintainable.

[ApiController]
[Route("[controller]")]
public class LoanProcessController : ControllerBase
{
private ICibilService _cibilService;
private IEmploymentService _employmentService;
private IAccountService _employmentService;
private IPaymentService _paymentService;
private IEMIService _emiService;
public LoanProcessController(ICibilService cibilService,
IEmploymentService employmentService,
IAccountService accountService,
IPaymentService paymentService,
IEMIService emiService)
{
_cibilService = cibilService;
_employmentService = employmentService;
_accountService = accountService;
_paymentService = paymentService;
_emiService = emiService;
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task<IEnumerable<LoanApplication>> ApproveLoan()
{
_cibilService.ValidateCibilScore();
_employmentService.ValidateEmployeeStatus();
_accountService.ValidateAccount();
_paymentService.ValidatePaymentHistory();
__emiService.ValidatePaymentEMIPaymentRatio();
}
}
builder.Services.AddMediatR(Assembly.GetExecutingAssembly());
using MediatR;namespace CQRSWebAPIDemo.CQRS.Query
{
public class GetWeatherForecastQueryRequest : IRequest<IEnumerable<WeatherForecast>>
{
private static readonly string[] Summaries = new[]{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public DateTime DateTime { get; set; }
public class GetWeatherForecastQueryHandler : IRequestHandler<GetWeatherForecastQueryRequest, IEnumerable<WeatherForecast>>
{
private ILogger<GetWeatherForecastQueryHandler> _logger;
public GetWeatherForecastQueryHandler(ILogger<GetWeatherForecastQueryHandler> logger)
{
_logger = logger;
}
public async Task<IEnumerable<WeatherForecast>> Handle(GetWeatherForecastQueryRequest query, CancellationToken cancellationToken)
{
var dateTime = query.DateTime;
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = dateTime.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray();
}
}
}
}
using CQRSWebAPIDemo.CQRS.Query;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace CQRSWebAPIDemo.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private IMediator _mediator;
public WeatherForecastController(IMediator mediator, ILogger<WeatherForecastController> logger)
{
_logger = logger;
_mediator = mediator;
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task<IEnumerable<WeatherForecast>> Get()
{
return await _mediator.Send(new GetWeatherForecastQueryRequest { DateTime = DateTime.Now });
}
}
}

No comments:

Post a Comment