我目前正在使用Mediatr 3中的Pipeline行为进行请求验证。我遇到的所有示例都抛出ValidationException,如果发生任何故障,而不是这样做,我想返回带有错误的响应。有人知道怎么做吗?
以下是验证管道的代码:
public class ValidationPipeline<TRequest, TResponse> :
IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;
public ValidationPipeline(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next)
{
var failures = _validators
.Select(v => v.Validate(request))
.SelectMany(result => result.Errors)
.Where(f => f != null)
.ToList();
if (failures.Any())
{
throw new ValidationException(failures);
}
return next();
}
}
注意:我发现了这个问题Handling errors/exceptions in a mediator pipeline using CQRS?,我对答案中的第一个选项感兴趣,但没有关于如何做到这一点的明确示例。
这是我的回复课程:
public class ResponseBase : ValidationResult
{
public ResponseBase() : base() { }
public ResponseBase(IEnumerable<ValidationFailure> failures) : base(failures) {
}
}
我在验证管道类中添加了以下签名:
public class ValidationPipeline<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
where TResponse : ResponseBase
我在Handle方法中做到了这一点:
var response = new ResponseBase(failures);
return Task.FromResult<TResponse>(response);
但这给了我错误'无法转换为TResponse'。
答案 0 :(得分:2)
几年前,我创建了常规的Result对象,并不断进行改进。非常简单,请检查https://github.com/martinbrabec/mbtools。
如果可以确定Result(或Result <>)是Application层中的返回类型每种方法,那么您可以使用ValidationBehavior,如下所示:
public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TResponse : Result, new()
{
private readonly IEnumerable<IValidator<TRequest>> _validators;
public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
if (_validators.Any())
{
var context = new ValidationContext(request);
List<ValidationFailure> failures = _validators
.Select(v => v.Validate(context))
.SelectMany(result => result.Errors)
.Where(f => f != null)
.ToList();
if (failures.Any())
{
TResponse response = new TResponse();
response.Set(ErrorType.NotValid, failures.Select(s => s.ErrorMessage), null);
return Task.FromResult<TResponse>(response);
}
else
{
return next();
}
}
return next();
}
}
由于所有处理程序都返回Result(或基于Result的Result <>),因此您将能够处理所有验证错误而没有任何异常。
答案 1 :(得分:1)
如果发生任何故障,请不要致电next
:
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next)
{
var failures = _validators
.Select(v => v.Validate(request))
.SelectMany(result => result.Errors)
.Where(f => f != null)
.ToList();
if (failures.Any())
{
var response = new Thing(); //obviously a type conforming to TResponse
response.Failures = failures; //I'm making an assumption on the property name here.
return Task.FromResult(response);
}
else
{
return next();
}
}
注意:
您的课程(我的示例中为Thing
)必须为TResponse
答案 2 :(得分:0)
您可以使用软件包配置验证处理 https://www.nuget.org/packages/MediatR.Extensions.FluentValidation.AspNetCore
只需插入配置部分:
services.AddFluentValidation(new[] {typeof(GenerateInvoiceHandler).GetTypeInfo().Assembly});