Skip to content

Commit

Permalink
Refactor LoggingBehaviour and ValidationBehaviour classes
Browse files Browse the repository at this point in the history
Removed unnecessary fields and constructors from both LoggingBehaviour and ValidationBehaviour classes, streamlining them for better readability and performance. Adjusted method implementations to directly use passed parameters instead of stored fields.
  • Loading branch information
Gary Woodfine committed Jan 16, 2024
1 parent 714fae6 commit a42cd61
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 35 deletions.
20 changes: 4 additions & 16 deletions src/Project/Behaviours/LoggingBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Serilog;
using ILogger = Serilog.ILogger;

namespace ApiProject.Behaviours
{
public class LoggingBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
public class LoggingBehaviour<TRequest, TResponse>(ILogger logger) : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly ILogger _logger;
public LoggingBehaviour(ILogger logger)
{
_logger = logger;
}


public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
//Query
_logger.Information($"Handling {typeof(TRequest).Name}");
logger.Information($"Handling {typeof(TRequest).Name}");
Type myType = request.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
object propValue = prop.GetValue(request, null);
_logger.Information("{Property} : {@Value}", prop.Name, propValue);
logger.Information("{Property} : {@Value}", prop.Name, propValue);
}
var response = await next();
//FilterResponse
_logger.Information($"Handled {typeof(TResponse).Name}");
logger.Information($"Handled {typeof(TResponse).Name}");
return response;
}
}


}
20 changes: 7 additions & 13 deletions src/Project/Behaviours/ValidationBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,21 @@

namespace ApiProject.Behaviours
{
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest,TResponse>
where TResponse : class where TRequest : IRequest<TResponse>
public class ValidationBehaviour<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators)
: IPipelineBehavior<TRequest, TResponse>
where TResponse : class
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;
private readonly ILogger _logger;

public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators, ILogger logger)
{
_validators = validators;
_logger = logger;
}


public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
if (!typeof(TResponse).IsGenericType) return await next();
if (!_validators.Any()) return await next();
if (!validators.Any()) return await next();

var context = new ValidationContext<TRequest>(request);
var validationResults =
await Task.WhenAll(_validators.Select(v => v.ValidateAsync(context, cancellationToken)));
await Task.WhenAll(validators.Select(v => v.ValidateAsync(context, cancellationToken)));
var failures = validationResults.SelectMany(r => r.Errors)
.Where(f => f != null)
.GroupBy(x => x.PropertyName,
Expand All @@ -42,7 +36,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
})
.ToDictionary(x => x.Key, x => x.Values);

if (!failures.Any()) return await next();
if (failures.Count == 0) return await next();

return Activator.CreateInstance(typeof(TResponse), null, failures.ToList()) as TResponse;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Solution/src/Api/Behaviours/LoggingBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ public class LoggingBehaviour<TRequest, TResponse>(ILogger logger) : IPipelineBe
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
//Query
_logger.Information($"Handling {typeof(TRequest).Name}");
logger.Information($"Handling {typeof(TRequest).Name}");
Type myType = request.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
object propValue = prop.GetValue(request, null);
_logger.Information("{Property} : {@Value}", prop.Name, propValue);
logger.Information("{Property} : {@Value}", prop.Name, propValue);
}
var response = await next();
//FilterResponse
_logger.Information($"Handled {typeof(TResponse).Name}");
logger.Information($"Handled {typeof(TResponse).Name}");
return response;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Solution/src/Api/Behaviours/ValidationBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class ValidationBehaviour<TRequest, TResponse>(IEnumerable<IValidator<TRe
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
if (!typeof(TResponse).IsGenericType) return await next();
if (!_validators.Any()) return await next();
if (!validators.Any()) return await next();

var context = new ValidationContext<TRequest>(request);
var validationResults =
await Task.WhenAll(_validators.Select(v => v.ValidateAsync(context, cancellationToken)));
await Task.WhenAll(validators.Select(v => v.ValidateAsync(context, cancellationToken)));
var failures = validationResults.SelectMany(r => r.Errors)
.Where(f => f != null)
.GroupBy(x => x.PropertyName,
Expand All @@ -30,7 +30,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
})
.ToDictionary(x => x.Key, x => x.Values);

if (!failures.Any()) return await next();
if (failures.Count == 0) return await next();

return Activator.CreateInstance(typeof(TResponse), null, failures.ToList()) as TResponse;
}
Expand Down

0 comments on commit a42cd61

Please sign in to comment.