From c8e20e9145dd4a95bb95c9cfa031b3ba4e662051 Mon Sep 17 00:00:00 2001 From: jasonmwebb-lv <97196139+jasonmwebb-lv@users.noreply.github.com> Date: Fri, 19 Apr 2024 00:34:56 -0600 Subject: [PATCH] Dev (#128) * Minor fix to build project. * Version bump. --- Build/Build.cs | 2 +- Build/Build.csproj | 25 ------------ .../Behaviors/ValidatorBehavior.cs | 38 ++++++++++++++++++- .../MediatRBuilderExtensions.cs | 1 + 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/Build/Build.cs b/Build/Build.cs index 389a67c0..26646579 100644 --- a/Build/Build.cs +++ b/Build/Build.cs @@ -134,7 +134,7 @@ protected override void OnBuildInitialized() { Log.Information("Generating NuGet packages for projects in solution"); int commitNum = 0; - string NuGetVersionCustom = "2.0.0.8"; + string NuGetVersionCustom = "2.0.0.10"; //if it's not a tagged release - append the commit number to the package version diff --git a/Build/Build.csproj b/Build/Build.csproj index 1bd56474..22b49174 100644 --- a/Build/Build.csproj +++ b/Build/Build.csproj @@ -21,29 +21,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs b/Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs index 15c3ad3c..059ba76f 100644 --- a/Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs +++ b/Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs @@ -1,6 +1,7 @@ using FluentValidation; using MediatR; using Microsoft.Extensions.Logging; +using RCommon.Mediator.Subscribers; using System; using System.Collections.Generic; using System.Linq; @@ -10,8 +11,42 @@ namespace RCommon.Mediator.MediatR.Behaviors { - public class ValidatorBehavior : IPipelineBehavior + public class ValidatorBehaviorForMediatR : IPipelineBehavior where TRequest : IRequest + { + private readonly ILogger> _logger; + private readonly IEnumerable> _validators; + + public ValidatorBehaviorForMediatR(IEnumerable> validators, ILogger> logger) + { + _validators = validators ?? throw new ArgumentNullException(nameof(validators)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) + { + var typeName = request.GetGenericTypeName(); + + _logger.LogInformation("----- Validating command {CommandType}", typeName); + + if (_validators.Any()) + { + var context = new ValidationContext(request); + var validationResults = await Task.WhenAll(_validators.Select(v => v.ValidateAsync(context, cancellationToken))); + var failures = validationResults.SelectMany(r => r.Errors).Where(f => f != null).ToList(); + if (failures.Count != 0) + { + _logger.LogWarning("Validation errors - {CommandType} - Command: {@Command} - Errors: {@ValidationErrors}", typeName, request, failures); + string message = $"Command Validation Errors for type {typeof(TRequest).Name}"; + throw new FluentValidation.ValidationException(message, failures); + } + } + return await next(); + } + } + + public class ValidatorBehavior : IPipelineBehavior + where TRequest : IAppRequest { private readonly ILogger> _logger; private readonly IEnumerable> _validators; @@ -43,4 +78,5 @@ public async Task Handle(TRequest request, RequestHandlerDelegate), typeof(ValidatorBehavior<,>)); + builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidatorBehaviorForMediatR<,>)); } public static void AddUnitOfWorkToRequestPipeline(this IMediatRBuilder builder)