diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs new file mode 100644 index 000000000..c2e04abe8 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs @@ -0,0 +1,23 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Ordering.API.Infrastructure.Behaviors +{ + public class LoggingBehavior : IPipelineBehavior + { + private readonly ILogger> _logger; + public LoggingBehavior(ILogger> logger) => _logger = logger; + + public async Task Handle(TRequest request, RequestHandlerDelegate next) + { + _logger.LogInformation($"Handling {typeof(TRequest).Name}"); + var response = await next(); + _logger.LogInformation($"Handled {typeof(TResponse).Name}"); + return response; + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Application/Decorators/ValidatorDecorator.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs similarity index 51% rename from src/Services/Ordering/Ordering.API/Application/Decorators/ValidatorDecorator.cs rename to src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs index 293ad6eb0..f8f582858 100644 --- a/src/Services/Ordering/Ordering.API/Application/Decorators/ValidatorDecorator.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs @@ -1,33 +1,23 @@ using FluentValidation; using MediatR; +using Microsoft.Extensions.Logging; using Ordering.Domain.Exceptions; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace Ordering.API.Application.Decorators +namespace Ordering.API.Infrastructure.Behaviors { - public class ValidatorDecorator - : IAsyncRequestHandler - where TRequest : IRequest + public class ValidatorBehavior : IPipelineBehavior { - private readonly IAsyncRequestHandler _inner; private readonly IValidator[] _validators; + public ValidatorBehavior(IValidator[] validators) => _validators = validators; - - public ValidatorDecorator( - IAsyncRequestHandler inner, - IValidator[] validators) - { - _inner = inner; - _validators = validators; - } - - public async Task Handle(TRequest message) + public async Task Handle(TRequest request, RequestHandlerDelegate next) { var failures = _validators - .Select(v => v.Validate(message)) + .Select(v => v.Validate(request)) .SelectMany(result => result.Errors) .Where(error => error != null) .ToList(); @@ -37,9 +27,8 @@ namespace Ordering.API.Application.Decorators throw new OrderingDomainException( $"Command Validation Errors for type {typeof(TRequest).Name}", new ValidationException("Validation exception", failures)); } - - var response = await _inner.Handle(message); + var response = await next(); return response; } } diff --git a/src/Services/Ordering/Ordering.API/Application/Decorators/LogDecorator.cs b/src/Services/Ordering/Ordering.API/Application/Decorators/LogDecorator.cs deleted file mode 100644 index cd0ca70ab..000000000 --- a/src/Services/Ordering/Ordering.API/Application/Decorators/LogDecorator.cs +++ /dev/null @@ -1,34 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Decorators -{ - using Extensions.Logging; - using MediatR; - using System.Threading.Tasks; - - public class LogDecorator - : IAsyncRequestHandler - where TRequest : IRequest - { - private readonly IAsyncRequestHandler _inner; - private readonly ILogger> _logger; - - - public LogDecorator( - IAsyncRequestHandler inner, - ILogger> logger) - { - _inner = inner; - _logger = logger; - } - - public async Task Handle(TRequest message) - { - _logger.LogInformation($"Executing command {_inner.GetType().FullName}"); - - var response = await _inner.Handle(message); - - _logger.LogInformation($"Command executed successfully {_inner.GetType().FullName}"); - - return response; - } - } -} diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs index a10abce54..86e18668b 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs @@ -3,11 +3,9 @@ using Autofac.Core; using FluentValidation; using MediatR; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; -using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Decorators; -using Ordering.API.Application.Decorators; using Ordering.API.Application.DomainEventHandlers.OrderStartedEvent; using Ordering.API.Application.Validations; -using Ordering.Domain.Events; +using Ordering.API.Infrastructure.Behaviors; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -33,7 +31,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Autof .Where(i => i.IsClosedTypeOf(typeof(IAsyncNotificationHandler<>))) .Select(i => new KeyedService("IAsyncNotificationHandler", i))) .AsImplementedInterfaces(); - + builder .RegisterAssemblyTypes(typeof(CreateOrderCommandValidator).GetTypeInfo().Assembly) @@ -45,25 +43,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Autof { var componentContext = context.Resolve(); return t => { object o; return componentContext.TryResolve(t, out o) ? o : null; }; - }); + }); builder.Register(context => { var componentContext = context.Resolve(); - return t => (IEnumerable)componentContext.Resolve(typeof(IEnumerable<>).MakeGenericType(t)); + return t => + { + var resolved = (IEnumerable)componentContext.Resolve(typeof(IEnumerable<>).MakeGenericType(t)); + return resolved; + }; }); - - - builder.RegisterGenericDecorator(typeof(LogDecorator<,>), - typeof(IAsyncRequestHandler<,>), - "IAsyncRequestHandler") - .Keyed("handlerDecorator", typeof(IAsyncRequestHandler<,>)); + builder.RegisterGeneric(typeof(LoggingBehavior<,>)).As(typeof(IPipelineBehavior<,>)); + builder.RegisterGeneric(typeof(ValidatorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); - builder.RegisterGenericDecorator(typeof(ValidatorDecorator<,>), - typeof(IAsyncRequestHandler<,>), - fromKey: "handlerDecorator"); } } }