From a42cd61b27c1e3b5ce014bc93f6fcc154efc3029 Mon Sep 17 00:00:00 2001 From: Gary Woodfine Date: Tue, 16 Jan 2024 13:33:27 +0000 Subject: [PATCH] Refactor LoggingBehaviour and ValidationBehaviour classes 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. --- src/Project/Behaviours/LoggingBehaviour.cs | 20 ++++--------------- src/Project/Behaviours/ValidationBehaviour.cs | 20 +++++++------------ .../src/Api/Behaviours/LoggingBehaviour.cs | 6 +++--- .../src/Api/Behaviours/ValidationBehaviour.cs | 6 +++--- 4 files changed, 17 insertions(+), 35 deletions(-) diff --git a/src/Project/Behaviours/LoggingBehaviour.cs b/src/Project/Behaviours/LoggingBehaviour.cs index b07baf7..fe02c99 100644 --- a/src/Project/Behaviours/LoggingBehaviour.cs +++ b/src/Project/Behaviours/LoggingBehaviour.cs @@ -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 : IPipelineBehavior + public class LoggingBehaviour(ILogger logger) : IPipelineBehavior where TRequest : IRequest { - private readonly ILogger _logger; - public LoggingBehaviour(ILogger logger) - { - _logger = logger; - } - public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { //Query - _logger.Information($"Handling {typeof(TRequest).Name}"); + logger.Information($"Handling {typeof(TRequest).Name}"); Type myType = request.GetType(); IList props = new List(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; } } - } \ No newline at end of file diff --git a/src/Project/Behaviours/ValidationBehaviour.cs b/src/Project/Behaviours/ValidationBehaviour.cs index d5b3cdd..c74df12 100644 --- a/src/Project/Behaviours/ValidationBehaviour.cs +++ b/src/Project/Behaviours/ValidationBehaviour.cs @@ -10,27 +10,21 @@ namespace ApiProject.Behaviours { - public class ValidationBehaviour : IPipelineBehavior - where TResponse : class where TRequest : IRequest + public class ValidationBehaviour(IEnumerable> validators) + : IPipelineBehavior + where TResponse : class + where TRequest : IRequest { - private readonly IEnumerable> _validators; - private readonly ILogger _logger; - - public ValidationBehaviour(IEnumerable> validators, ILogger logger) - { - _validators = validators; - _logger = logger; - } public async Task Handle(TRequest request, RequestHandlerDelegate 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(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, @@ -42,7 +36,7 @@ public async Task Handle(TRequest request, RequestHandlerDelegate 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; } diff --git a/src/Solution/src/Api/Behaviours/LoggingBehaviour.cs b/src/Solution/src/Api/Behaviours/LoggingBehaviour.cs index 1219509..4959d0c 100644 --- a/src/Solution/src/Api/Behaviours/LoggingBehaviour.cs +++ b/src/Solution/src/Api/Behaviours/LoggingBehaviour.cs @@ -11,17 +11,17 @@ public class LoggingBehaviour(ILogger logger) : IPipelineBe public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { //Query - _logger.Information($"Handling {typeof(TRequest).Name}"); + logger.Information($"Handling {typeof(TRequest).Name}"); Type myType = request.GetType(); IList props = new List(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; } } diff --git a/src/Solution/src/Api/Behaviours/ValidationBehaviour.cs b/src/Solution/src/Api/Behaviours/ValidationBehaviour.cs index a5cedf8..c323460 100644 --- a/src/Solution/src/Api/Behaviours/ValidationBehaviour.cs +++ b/src/Solution/src/Api/Behaviours/ValidationBehaviour.cs @@ -14,11 +14,11 @@ public class ValidationBehaviour(IEnumerable Handle(TRequest request, RequestHandlerDelegate 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(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, @@ -30,7 +30,7 @@ public async Task Handle(TRequest request, RequestHandlerDelegate 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; }