Refactor validation behavior and update result view command
Refactored ValidationBehavior to use C# 12 primary constructors and file-scoped namespaces, simplifying the Handle method logic. Updated CreateResultViewCommand to implement IAuthScoped and IRequest, replaced AddedWho with a non-serialized Scope property, and added necessary usings and namespace.
This commit is contained in:
@@ -1,30 +1,29 @@
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace ReC.Application.Common.Behaviors
|
||||
namespace ReC.Application.Common.Behaviors;
|
||||
|
||||
public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : notnull
|
||||
{
|
||||
public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : notnull
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancel)
|
||||
{
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancel)
|
||||
if (validators.Any())
|
||||
{
|
||||
if (validators.Any())
|
||||
{
|
||||
var context = new ValidationContext<TRequest>(request);
|
||||
var context = new ValidationContext<TRequest>(request);
|
||||
|
||||
var validationResults = await Task.WhenAll(
|
||||
validators.Select(v =>
|
||||
v.ValidateAsync(context, cancel)));
|
||||
var validationResults = await Task.WhenAll(
|
||||
validators.Select(v =>
|
||||
v.ValidateAsync(context, cancel)));
|
||||
|
||||
var failures = validationResults
|
||||
.SelectMany(r => r.Errors)
|
||||
.Where(f => f != null)
|
||||
.ToList();
|
||||
var failures = validationResults
|
||||
.SelectMany(r => r.Errors)
|
||||
.Where(f => f != null)
|
||||
.ToList();
|
||||
|
||||
if (failures.Count != 0)
|
||||
throw new ValidationException(failures);
|
||||
}
|
||||
return await next(cancel);
|
||||
if (failures.Count != 0)
|
||||
throw new ValidationException(failures);
|
||||
}
|
||||
return await next(cancel);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user