Updated PHASENPLAN and ROADMAP to reflect progress on Feature 1 - ValidatePDF (75% complete). Marked Step 1.1 as completed, including MediatR setup, pipeline behaviors (`ValidationBehavior`, `LoggingBehavior`), ValidatePDF feature (Query, Handler, Validator), and DTOs. Added `DependencyInjection.cs` for Application Layer DI configuration. Introduced `LoggingBehavior` and `ValidationBehavior` for MediatR pipelines. Implemented `ValidatePdfHandler`, `ValidatePdfQuery`, and `ValidatePdfValidator`. Created DTOs (`ValidatePdfRequest`, `ValidatePdfResponse`) for the ValidatePDF feature. Added unit tests for `ValidatePdfHandler` to verify metadata handling and exception propagation. Removed unused folder references in `DocumentOperator.Application.csproj`.
55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using FluentValidation;
|
|
using MediatR;
|
|
|
|
namespace DocumentOperator.Application.Common.Behaviors;
|
|
|
|
/// <summary>
|
|
/// MediatR Pipeline Behavior that validates requests using FluentValidation
|
|
/// Executes BEFORE the Handler
|
|
/// </summary>
|
|
public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
|
where TRequest : IRequest<TResponse>
|
|
{
|
|
private readonly IEnumerable<IValidator<TRequest>> _validators;
|
|
|
|
public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
|
|
{
|
|
_validators = validators;
|
|
}
|
|
|
|
public async Task<TResponse> Handle(
|
|
TRequest request,
|
|
RequestHandlerDelegate<TResponse> next,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
// Wenn keine Validators registriert sind, direkt weiter zum Handler
|
|
if (!_validators.Any())
|
|
{
|
|
return await next();
|
|
}
|
|
|
|
// Validation Context erstellen
|
|
var context = new ValidationContext<TRequest>(request);
|
|
|
|
// Alle Validators parallel ausführen
|
|
var validationResults = await Task.WhenAll(
|
|
_validators.Select(v => v.ValidateAsync(context, cancellationToken))
|
|
);
|
|
|
|
// Fehler sammeln
|
|
var failures = validationResults
|
|
.Where(r => !r.IsValid)
|
|
.SelectMany(r => r.Errors)
|
|
.ToList();
|
|
|
|
// Bei Fehlern: ValidationException werfen (wird von Exception Middleware gefangen)
|
|
if (failures.Any())
|
|
{
|
|
throw new ValidationException(failures);
|
|
}
|
|
|
|
// Validation erfolgreich ? weiter zum Handler
|
|
return await next();
|
|
}
|
|
}
|