Skip to content

Commit

Permalink
Dev (#128)
Browse files Browse the repository at this point in the history
* Minor fix to build project.

* Version bump.
  • Loading branch information
jasonmwebb-lv authored Apr 19, 2024
1 parent d256b3f commit c8e20e9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 0 additions & 25 deletions Build/Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,4 @@
<ItemGroup>
<PackageDownload Include="GitVersion.Tool" Version="[5.12.0]" />
</ItemGroup>

<ItemGroup>
<None Remove="..\.nuke\temp\build.2024-04-18_22-16-52.log" />
</ItemGroup>

<ItemGroup>
<None Remove="..\.nuke\temp\build.2024-04-18_22-36-15.log" />
</ItemGroup>

<ItemGroup>
<None Remove="..\.nuke\temp\build.2024-04-18_22-42-12.log" />
</ItemGroup>

<ItemGroup>
<None Remove="..\.nuke\temp\build.2024-04-18_22-51-19.log" />
</ItemGroup>

<ItemGroup>
<None Remove="..\.nuke\temp\build.2024-04-18_22-52-29.log" />
</ItemGroup>

<ItemGroup>
<None Remove="..\.nuke\temp\build.2024-04-18_22-53-22.log" />
</ItemGroup>

</Project>
38 changes: 37 additions & 1 deletion Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,8 +11,42 @@

namespace RCommon.Mediator.MediatR.Behaviors
{
public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
public class ValidatorBehaviorForMediatR<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly ILogger<ValidatorBehaviorForMediatR<TRequest, TResponse>> _logger;
private readonly IEnumerable<IValidator<TRequest>> _validators;

public ValidatorBehaviorForMediatR(IEnumerable<IValidator<TRequest>> validators, ILogger<ValidatorBehaviorForMediatR<TRequest, TResponse>> logger)
{
_validators = validators ?? throw new ArgumentNullException(nameof(validators));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var typeName = request.GetGenericTypeName();

_logger.LogInformation("----- Validating command {CommandType}", typeName);

if (_validators.Any())
{
var context = new ValidationContext<TRequest>(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<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IAppRequest<TResponse>
{
private readonly ILogger<ValidatorBehavior<TRequest, TResponse>> _logger;
private readonly IEnumerable<IValidator<TRequest>> _validators;
Expand Down Expand Up @@ -43,4 +78,5 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
return await next();
}
}

}
1 change: 1 addition & 0 deletions Src/RCommon.Mediatr/MediatRBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static void AddLoggingToRequestPipeline(this IMediatRBuilder builder)
public static void AddValidationToRequestPipeline(this IMediatRBuilder builder)
{
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>));
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidatorBehaviorForMediatR<,>));
}

public static void AddUnitOfWorkToRequestPipeline(this IMediatRBuilder builder)
Expand Down

0 comments on commit c8e20e9

Please sign in to comment.