From 3556a5d126d2c58efd2c0fd24bcfd2754ae92261 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Tue, 16 May 2017 18:40:34 +0200 Subject: [PATCH] Change SaveEntitiesAsync to SaveChangesAsync --- ...OrderStatusChangedToPaidDomainEventHandler.cs | 5 ----- .../OrderPaymentFailedIntegrationEventHandler.cs | 4 ++-- ...rderPaymentSuccededIntegrationEventHandler.cs | 4 ++-- ...OrderStockConfirmedIntegrationEventHandler.cs | 4 ++-- ...erStockNotConfirmedIntegrationEventHandler.cs | 2 +- src/Services/Ordering/Ordering.API/Startup.cs | 16 +++++++++++----- .../OrderAggregate/IOrderRepository.cs | 2 ++ .../AggregatesModel/OrderAggregate/Order.cs | 2 +- .../Repositories/OrderRepository.cs | 5 +++++ 9 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs index 1eb4a8953..3aff01578 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs @@ -41,11 +41,6 @@ orderStockList); await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(decrementOrderStockCommandMsg); await _orderingIntegrationEventService.PublishThroughEventBusAsync(decrementOrderStockCommandMsg); - - //is it necessary get a DecrementOrderStockSuccessIntegrationEvent/DecrementOrderStockFailedIntegrationEvent before to call ShipOrderCommandMsg??? - var shipOrderCommandMsg = new ShipOrderCommandMsg(orderStatusChangedToPaidDomainEvent.OrderId); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(shipOrderCommandMsg); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(shipOrderCommandMsg); } } } \ No newline at end of file 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 259b7ec34..49b252e4a 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -17,11 +17,11 @@ public async Task Handle(OrderPaymentFailedIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); orderToUpdate.SetCancelledStatus(); - await _orderRepository.UnitOfWork.SaveEntitiesAsync(); + await _orderRepository.UnitOfWork.SaveChangesAsync(); } } } 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 0e8598dcc..fc813ff2d 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -17,11 +17,11 @@ public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); orderToUpdate.SetPaidStatus(); - await _orderRepository.UnitOfWork.SaveEntitiesAsync(); + await _orderRepository.UnitOfWork.SaveChangesAsync(); } } } \ 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 fa7463041..172444932 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -17,11 +17,11 @@ public async Task Handle(OrderStockConfirmedIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); orderToUpdate.SetStockConfirmedStatus(); - await _orderRepository.UnitOfWork.SaveEntitiesAsync(); + await _orderRepository.UnitOfWork.SaveChangesAsync(); } } } \ 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 b38b56b21..918e4f094 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs @@ -19,7 +19,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); var orderStockNotConfirmedItems = @event.OrderStockItems .FindAll(c => !c.Confirmed) diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index 95014dd5b..e2b0734f7 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -32,6 +32,7 @@ using System; using System.Data.Common; using System.Reflection; + using global::Ordering.API.Application.IntegrationEvents.EventHandling; public class Startup { @@ -129,8 +130,14 @@ services.AddSingleton(); services.AddTransient>(); services.AddTransient, OrderProcessSaga>(); - services.AddTransient>(); - services.AddTransient>(); + services.AddTransient, + OrderStockConfirmedIntegrationEventHandler>(); + services.AddTransient, + OrderStockNotConfirmedIntegrationEventHandler>(); + services.AddTransient, + OrderPaymentFailedIntegrationEventHandler>(); + services.AddTransient, + OrderPaymentSuccededIntegrationEventHandler>(); services.AddOptions(); //configure autofac @@ -176,12 +183,11 @@ var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe>(); - eventBus.Subscribe>(); - eventBus.Subscribe>(); - eventBus.Subscribe>(); + eventBus.Subscribe>(); + eventBus.Subscribe>(); } protected virtual void ConfigureAuth(IApplicationBuilder app) diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs index d7346ee4f..368410d62 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs @@ -13,5 +13,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O void Update(Order order); Task GetAsync(int orderId); + + Task GetWithDependenciesAsync(int orderId); } } diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs index 518f22d9c..51c201e6b 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 IReadOnlyList OrderItems => _orderItems; + public IReadOnlyCollection 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) diff --git a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs index 1dbc00690..df7b07aaa 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs @@ -32,6 +32,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor } public async Task GetAsync(int orderId) + { + return await _context.Orders.FindAsync(orderId); + } + + public async Task GetWithDependenciesAsync(int orderId) { return await _context.Orders .Include(c => c.OrderStatus)