返回Mediator管道行为中有错误的响应



我是MediatR的新手,试图使用管道行为进行请求验证,我遇到的所有例子都是在发生任何错误时抛出ValidationException
下面的代码是验证管道的示例:

public class ValidationPipeline<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator> _validators;
public ValidationPipeline(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var context = new ValidationContext<TRequest>(request);
var validationFailures = _validators
.Select(validator => validator.Validate(context))
.SelectMany(validationResult => validationResult.Errors)
.Where(validationFailure => validationFailure != null)
.ToList();
if (validationFailures.Any())
{
throw new FluentValidation.ValidationException(validationFailures);
}

return next();
}
}

这个方法工作得很好,但我想返回带有验证错误的响应(不(抛出异常,所以我尝试了这个:

public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, BaseResponse<TResponse>>
where TRequest : IRequest<BaseResponse<TResponse>>
{
private readonly IEnumerable<IValidator> _validators;
public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public Task<BaseResponse<TResponse>> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<BaseResponse<TResponse>> next)
{
var context = new ValidationContext<TRequest>(request);
var validationFailures = _validators
.Select(validator => validator.Validate(context))
.SelectMany(validationResult => validationResult.Errors)
.Where(validationFailure => validationFailure != null)
.ToList();
if (validationFailures.Any())
{
return Task.FromResult(new BaseResponse<TResponse>
{
Code = 400,
Message = "Validation error",
Error = validationFailures.Select(err => err.ErrorMessage)
});
}
else
{
return next();
}
}

但现在验证管道代码不执行,
执行转到常规处理程序(例如:注册用户处理程序(。

我的响应(用于所有处理程序(:

public class BaseResponse<TResponse>
{
public int Code { get; set; }
public string Message { get; set; }
public TResponse Result { get; set; }
public object Error { get; set; }
public string TraceIdentifier { get; set; }
}

用DI注册行为,如下所示:

services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));

任何帮助都将不胜感激。

它不会触发,因为这个管道不再匹配您的IRequest。在您的情况下,TResponse已经是BaseResponse<gt然后你再把它包起来。

我假设您有以下请求结构:

public record TestDto(string Result);
public class TestCommand(int Id, string Name) : IRequest<BaseResponse<TestDto>>;
public class TestCommandHandler : IRequestHandler<TestCommand, BaseResponse<TestDto>>
{
public async Task<BaseResponse<TestDto>> Handle(TestCommand request, CancellationToken cancellationToken)
{
...
}
}

在这种情况下,TResponseBaseResponse<测试Dto>

要解决此问题,您可以执行以下操作:

将带有参数的构造函数添加到BaseResponse<T>像这样:

public class BaseResponse<TResponse>
{
public int Code { get; set; }
public string Message { get; set; }
public TResponse Result { get; set; }
public object Error { get; set; }
public string TraceIdentifier { get; set; }
public BaseResponse()
{
}
public BaseResponse(int code, string message, object error)
{
Code = code;
Message = message;
Error = error;
}
}

如果验证失败,则必须创建此对象。您可以使用Activator来实现这一点。

public class ValidationPipeline<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator> _validators;
public ValidationPipeline(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var context = new ValidationContext<TRequest>(request);
var validationFailures = _validators
.Select(validator => validator.Validate(context))
.SelectMany(validationResult => validationResult.Errors)
.Where(validationFailure => validationFailure != null)
.ToList();
if (validationFailures.Any())
{
var code = 400;
var message = "Validation error";
var error = validationFailures.Select(err => err.ErrorMessage);
return (TResponse)Activator.CreateInstance(typeof(TResponse),
code, 
message, 
error);
}

return next();
}
}

我使用了下一个方法。首先,我使用Adrdalis.Result或通过nuget-Ardalist。结果我发现它非常有用。管道代码:

public class FluentValidationPipelineBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;
public FluentValidationPipelineBehaviour(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var requestType = request.GetType();
if (requestType != null)
{
var attribute = requestType.GetCustomAttribute<FluentValidationAttribute>();
if (attribute != null && attribute.IsEnabled)
{
var context = new ValidationContext<TRequest>(request);
var validationResults = await Task.WhenAll(
_validators.Select(v =>
v.ValidateAsync(context, cancellationToken)));
var failures = validationResults
.Where(r => r.Errors.Any())
.SelectMany(r => r.Errors)
.ToList();

if (failures.Any())
{
if (attribute.ThrowExceptionOnError)
{
throw new ValidationException(failures);
}
return GetValidatableResult(failures.AsErrors());
}
}
}
return await next();
}
private static TResponse GetValidatableResult(List<ValidationError> validationErrors)
{
#pragma warning disable CS8603
#pragma warning disable CS8602
#pragma warning disable CS8600
return (TResponse)(typeof(Result<>).MakeGenericType(typeof(TResponse).GetGenericArguments())
.GetMethod("Invalid").Invoke(null, new object?[] { validationErrors }));
#pragma warning restore CS8600
#pragma warning restore CS8602
#pragma warning restore CS8603
}
}

我正在使用FluentValidationAttribute来配置fluentvalidation行为

[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class FluentValidationAttribute : Attribute
{
public bool IsEnabled { get; set; } = true;
public bool ThrowExceptionOnError { get; set; } = false;
}

在命令/查询等方面使用它;

相关内容

  • 没有找到相关文章

最新更新