commit
29996fc94d
@ -1,8 +1,6 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Infrastructure.Behaviors
|
||||
@ -12,7 +10,7 @@ namespace Ordering.API.Infrastructure.Behaviors
|
||||
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
|
||||
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger) => _logger = logger;
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next)
|
||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||
{
|
||||
_logger.LogInformation($"Handling {typeof(TRequest).Name}");
|
||||
var response = await next();
|
||||
|
@ -1,10 +1,9 @@
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ordering.Domain.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Infrastructure.Behaviors
|
||||
@ -14,7 +13,7 @@ namespace Ordering.API.Infrastructure.Behaviors
|
||||
private readonly IValidator<TRequest>[] _validators;
|
||||
public ValidatorBehavior(IValidator<TRequest>[] validators) => _validators = validators;
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next)
|
||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||
{
|
||||
var failures = _validators
|
||||
.Select(v => v.Validate(request))
|
||||
|
@ -2,15 +2,13 @@
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Application.Commands
|
||||
{
|
||||
// Regular CommandHandler
|
||||
public class CancelOrderCommandHandler : IAsyncRequestHandler<CancelOrderCommand, bool>
|
||||
public class CancelOrderCommandHandler : IRequestHandler<CancelOrderCommand, bool>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
|
||||
@ -25,7 +23,7 @@ namespace Ordering.API.Application.Commands
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Handle(CancelOrderCommand command)
|
||||
public async Task<bool> Handle(CancelOrderCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
|
||||
if(orderToUpdate == null)
|
||||
|
@ -5,11 +5,12 @@
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// Regular CommandHandler
|
||||
public class CreateOrderCommandHandler
|
||||
: IAsyncRequestHandler<CreateOrderCommand, bool>
|
||||
: IRequestHandler<CreateOrderCommand, bool>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly IIdentityService _identityService;
|
||||
@ -23,7 +24,7 @@
|
||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(CreateOrderCommand message)
|
||||
public async Task<bool> Handle(CreateOrderCommand message, CancellationToken cancellationToken)
|
||||
{
|
||||
// Add/Update the Buyer AggregateRoot
|
||||
// DDD patterns comment: Add child entities and value-objects through the Order Aggregate-Root
|
||||
|
@ -1,5 +1,6 @@
|
||||
using MediatR;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||
@ -10,7 +11,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the command handler that performs the operation if request is not duplicated</typeparam>
|
||||
/// <typeparam name="R">Return value of the inner command handler</typeparam>
|
||||
public class IdentifiedCommandHandler<T, R> : IAsyncRequestHandler<IdentifiedCommand<T, R>, R>
|
||||
public class IdentifiedCommandHandler<T, R> : IRequestHandler<IdentifiedCommand<T, R>, R>
|
||||
where T : IRequest<R>
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
@ -37,7 +38,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||
/// </summary>
|
||||
/// <param name="message">IdentifiedCommand which contains both original command & request ID</param>
|
||||
/// <returns>Return value of inner command or default value if request same ID was found</returns>
|
||||
public async Task<R> Handle(IdentifiedCommand<T, R> message)
|
||||
public async Task<R> Handle(IdentifiedCommand<T, R> message, CancellationToken cancellationToken)
|
||||
{
|
||||
var alreadyExists = await _requestManager.ExistAsync(message.Id);
|
||||
if (alreadyExists)
|
||||
@ -55,6 +56,4 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -2,12 +2,13 @@
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Application.Commands
|
||||
{
|
||||
// Regular CommandHandler
|
||||
public class ShipOrderCommandHandler : IAsyncRequestHandler<ShipOrderCommand, bool>
|
||||
public class ShipOrderCommandHandler : IRequestHandler<ShipOrderCommand, bool>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
|
||||
@ -22,7 +23,7 @@ namespace Ordering.API.Application.Commands
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Handle(ShipOrderCommand command)
|
||||
public async Task<bool> Handle(ShipOrderCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
|
||||
if(orderToUpdate == null)
|
||||
|
@ -1,16 +1,15 @@
|
||||
using MediatR;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ordering.API.Application.IntegrationEvents;
|
||||
using Ordering.API.Application.IntegrationEvents.Events;
|
||||
using Ordering.Domain.Events;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVerified
|
||||
{
|
||||
public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler
|
||||
: IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
|
||||
: INotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly ILoggerFactory _logger;
|
||||
@ -25,7 +24,7 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri
|
||||
// Domain Logic comment:
|
||||
// When the Buyer and Buyer's payment method have been created or verified that they existed,
|
||||
// then we can update the original Order with the BuyerId and PaymentId (foreign keys)
|
||||
public async Task Handle(BuyerAndPaymentMethodVerifiedDomainEvent buyerPaymentMethodVerifiedEvent)
|
||||
public async Task Handle(BuyerAndPaymentMethodVerifiedDomainEvent buyerPaymentMethodVerifiedEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(buyerPaymentMethodVerifiedEvent.OrderId);
|
||||
orderToUpdate.SetBuyerId(buyerPaymentMethodVerifiedEvent.Buyer.Id);
|
||||
|
@ -9,9 +9,10 @@
|
||||
using Ordering.API.Application.IntegrationEvents;
|
||||
using System.Linq;
|
||||
using Ordering.API.Application.IntegrationEvents.Events;
|
||||
using System.Threading;
|
||||
|
||||
public class OrderStatusChangedToAwaitingValidationDomainEventHandler
|
||||
: IAsyncNotificationHandler<OrderStatusChangedToAwaitingValidationDomainEvent>
|
||||
: INotificationHandler<OrderStatusChangedToAwaitingValidationDomainEvent>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly ILoggerFactory _logger;
|
||||
@ -26,11 +27,11 @@
|
||||
_orderingIntegrationEventService = orderingIntegrationEventService;
|
||||
}
|
||||
|
||||
public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent orderStatusChangedToAwaitingValidationDomainEvent)
|
||||
public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent orderStatusChangedToAwaitingValidationDomainEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.CreateLogger(nameof(OrderStatusChangedToAwaitingValidationDomainEvent))
|
||||
.LogTrace($"Order with Id: {orderStatusChangedToAwaitingValidationDomainEvent.OrderId} has been successfully updated with " +
|
||||
$"a status order id: {OrderStatus.AwaitingValidation.Id}");
|
||||
.LogTrace($"Order with Id: {orderStatusChangedToAwaitingValidationDomainEvent.OrderId} has been successfully updated with " +
|
||||
$"a status order id: {OrderStatus.AwaitingValidation.Id}");
|
||||
|
||||
var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems
|
||||
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
|
||||
|
@ -9,9 +9,10 @@
|
||||
using Ordering.API.Application.IntegrationEvents;
|
||||
using System.Linq;
|
||||
using Ordering.API.Application.IntegrationEvents.Events;
|
||||
using System.Threading;
|
||||
|
||||
public class OrderStatusChangedToPaidDomainEventHandler
|
||||
: IAsyncNotificationHandler<OrderStatusChangedToPaidDomainEvent>
|
||||
: INotificationHandler<OrderStatusChangedToPaidDomainEvent>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly ILoggerFactory _logger;
|
||||
@ -26,11 +27,11 @@
|
||||
_orderingIntegrationEventService = orderingIntegrationEventService;
|
||||
}
|
||||
|
||||
public async Task Handle(OrderStatusChangedToPaidDomainEvent orderStatusChangedToPaidDomainEvent)
|
||||
public async Task Handle(OrderStatusChangedToPaidDomainEvent orderStatusChangedToPaidDomainEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.CreateLogger(nameof(OrderStatusChangedToPaidDomainEventHandler))
|
||||
.LogTrace($"Order with Id: {orderStatusChangedToPaidDomainEvent.OrderId} has been successfully updated with " +
|
||||
$"a status order id: {OrderStatus.Paid.Id}");
|
||||
.LogTrace($"Order with Id: {orderStatusChangedToPaidDomainEvent.OrderId} has been successfully updated with " +
|
||||
$"a status order id: {OrderStatus.Paid.Id}");
|
||||
|
||||
var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems
|
||||
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
|
||||
|
@ -4,12 +4,13 @@ using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.Buyer
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ordering.Domain.Events;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
|
||||
{
|
||||
public class ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler
|
||||
: IAsyncNotificationHandler<OrderStartedDomainEvent>
|
||||
: INotificationHandler<OrderStartedDomainEvent>
|
||||
{
|
||||
private readonly ILoggerFactory _logger;
|
||||
private readonly IBuyerRepository _buyerRepository;
|
||||
@ -22,7 +23,7 @@ namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public async Task Handle(OrderStartedDomainEvent orderStartedEvent)
|
||||
public async Task Handle(OrderStartedDomainEvent orderStartedEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;
|
||||
var buyer = await _buyerRepository.FindAsync(orderStartedEvent.UserId);
|
||||
|
@ -8,9 +8,10 @@
|
||||
using System.Threading.Tasks;
|
||||
using Ordering.API.Application.IntegrationEvents;
|
||||
using Ordering.API.Application.IntegrationEvents.Events;
|
||||
using System.Threading;
|
||||
|
||||
public class OrderStatusChangedToStockConfirmedDomainEventHandler
|
||||
: IAsyncNotificationHandler<OrderStatusChangedToStockConfirmedDomainEvent>
|
||||
: INotificationHandler<OrderStatusChangedToStockConfirmedDomainEvent>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly ILoggerFactory _logger;
|
||||
@ -25,7 +26,7 @@
|
||||
_orderingIntegrationEventService = orderingIntegrationEventService;
|
||||
}
|
||||
|
||||
public async Task Handle(OrderStatusChangedToStockConfirmedDomainEvent orderStatusChangedToStockConfirmedDomainEvent)
|
||||
public async Task Handle(OrderStatusChangedToStockConfirmedDomainEvent orderStatusChangedToStockConfirmedDomainEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.CreateLogger(nameof(OrderStatusChangedToStockConfirmedDomainEventHandler))
|
||||
.LogTrace($"Order with Id: {orderStatusChangedToStockConfirmedDomainEvent.OrderId} has been successfully updated with " +
|
||||
|
@ -21,11 +21,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Autof
|
||||
|
||||
// Register all the Command classes (they implement IAsyncRequestHandler) in assembly holding the Commands
|
||||
builder.RegisterAssemblyTypes(typeof(CreateOrderCommand).GetTypeInfo().Assembly)
|
||||
.AsClosedTypesOf(typeof(IAsyncRequestHandler<,>));
|
||||
.AsClosedTypesOf(typeof(IRequestHandler<,>));
|
||||
|
||||
// Register the DomainEventHandler classes (they implement IAsyncNotificationHandler<>) in assembly holding the Domain Events
|
||||
builder.RegisterAssemblyTypes(typeof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler).GetTypeInfo().Assembly)
|
||||
.AsClosedTypesOf(typeof(IAsyncNotificationHandler<>));
|
||||
.AsClosedTypesOf(typeof(INotificationHandler<>));
|
||||
|
||||
// Register the Command's Validators (Validators based on FluentValidation library)
|
||||
builder
|
||||
|
@ -31,14 +31,14 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation.AspNetCore" Version="7.2.1" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="3.0.0" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="4.0.0" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.0" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.4.1" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.0.0-beta1" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.ServiceFabric" Version="2.0.1-beta1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
|
||||
<PackageReference Include="MediatR" Version="3.0.1" />
|
||||
<PackageReference Include="MediatR" Version="4.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.1.0" />
|
||||
<PackageReference Include="System.Reflection" Version="4.3.0" />
|
||||
<PackageReference Include="Dapper" Version="1.50.4" />
|
||||
|
@ -5,8 +5,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MediatR" Version="3.0.1" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="3.0.0" />
|
||||
<PackageReference Include="MediatR" Version="4.0.1" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="4.0.0" />
|
||||
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user