From d59ee8e817d167b8bb0beec1830c1fc009f98350 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Tue, 16 May 2017 15:11:03 +0200 Subject: [PATCH] SaveChangesAsync() from Integration events --- ...derPaymentFailedIntegrationEventHandler.cs | 2 ++ ...rPaymentSuccededIntegrationEventHandler.cs | 2 ++ ...erStockConfirmedIntegrationEventHandler.cs | 2 ++ ...tockNotConfirmedIntegrationEventHandler.cs | 2 ++ .../Application/Sagas/OrderProcessSaga.cs | 5 ++--- .../AggregatesModel/OrderAggregate/Order.cs | 19 ++++++++----------- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs index 1e4e79978..259b7ec34 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -20,6 +20,8 @@ var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); orderToUpdate.SetCancelledStatus(); + + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs index f3075eeec..0e8598dcc 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -20,6 +20,8 @@ var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); orderToUpdate.SetPaidStatus(); + + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs index 60c8bb65d..fa7463041 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -20,6 +20,8 @@ var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); orderToUpdate.SetStockConfirmedStatus(); + + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs index 68a89885c..b38b56b21 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs @@ -26,6 +26,8 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling .Select(c => c.ProductId); orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems); + + await _orderRepository.UnitOfWork.SaveChangesAsync(); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs index 12e6a3fa2..f230ba6fb 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs @@ -43,15 +43,14 @@ namespace Ordering.API.Application.Sagas /// period has completed. /// /// - public async Task Handle(ConfirmGracePeriodCommandMsg @event) + public async Task Handle(ConfirmGracePeriodCommandMsg command) { - var orderSaga = FindSagaById(@event.OrderId); + var orderSaga = FindSagaById(command.OrderId); CheckValidSagaId(orderSaga); if (orderSaga.OrderStatus != OrderStatus.Cancelled) { orderSaga.SetAwaitingValidationStatus(); - await SaveChangesAsync(); } } diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs index 64bb2d21d..518f22d9c 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs @@ -30,7 +30,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O // but only through the method OrderAggrergateRoot.AddOrderItem() which includes behaviour. private readonly List _orderItems; - public IEnumerable OrderItems => _orderItems.AsReadOnly(); + public IReadOnlyList OrderItems => _orderItems; // Using List<>.AsReadOnly() // This will create a read only wrapper around the private list so is protected against "external updates". // It's much cheaper than .ToList() because it will not have to copy all items in a new collection. (Just one heap alloc for the wrapper instance) @@ -107,9 +107,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O StatusChangeException(); } - _orderStatusId = OrderStatus.AwaitingValidation.Id; + AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, _orderItems)); - AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, OrderItems)); + _orderStatusId = OrderStatus.AwaitingValidation.Id; } public void SetStockConfirmedStatus(IEnumerable orderStockNotConfirmedItems = null) @@ -121,15 +121,14 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O if (orderStockNotConfirmedItems is null) { - OrderStatus = OrderStatus.StockConfirmed; + AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id)); + _orderStatusId = OrderStatus.StockConfirmed.Id; _description = "All the items were confirmed with available stock."; - - AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id)); } else { - OrderStatus = OrderStatus.Cancelled; + _orderStatusId = OrderStatus.Cancelled.Id; var itemsStockNotConfirmedProductNames = OrderItems .Where(c => orderStockNotConfirmedItems.Contains(c.ProductId)) @@ -147,10 +146,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O StatusChangeException(); } + AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems)); + _orderStatusId = OrderStatus.Paid.Id; _description = "The payment was performed at a simulated \"American Bank checking bank account endinf on XX35071\""; - - AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems)); } public void SetShippedStatus() @@ -162,8 +161,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O _orderStatusId = OrderStatus.Shipped.Id; _description = ""; - - //Call Domain Event } public void SetCancelledStatus()