mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-11 19:02:15 +08:00
30 lines
884 B
C#
30 lines
884 B
C#
using FluentValidation;
|
|
using MediatR;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace BuildingBlocks.Validation;
|
|
|
|
public sealed class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
|
where TRequest : class, IRequest<TResponse>
|
|
{
|
|
private IValidator<TRequest> _validator;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
public ValidationBehavior(IServiceProvider serviceProvider)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
}
|
|
|
|
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
_validator = _serviceProvider.GetService<IValidator<TRequest>>();
|
|
if (_validator is null)
|
|
return await next();
|
|
|
|
await _validator.HandleValidationAsync(request);
|
|
|
|
return await next();
|
|
}
|
|
}
|