remove SaveEntities and move the functionality overriding the SaveChanges method

This commit is contained in:
ericuss 2020-04-07 17:52:21 +02:00
parent c948bee891
commit 2d826edc88
10 changed files with 38 additions and 21 deletions

View File

@ -33,7 +33,8 @@ namespace Ordering.API.Application.Commands
} }
orderToUpdate.SetCancelledStatus(); orderToUpdate.SetCancelledStatus();
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return true;
} }
} }

View File

@ -57,8 +57,8 @@
_orderRepository.Add(order); _orderRepository.Add(order);
return await _orderRepository.UnitOfWork await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
.SaveEntitiesAsync(cancellationToken); return true;
} }
} }

View File

@ -27,13 +27,14 @@ namespace Ordering.API.Application.Commands
public async Task<bool> Handle(SetAwaitingValidationOrderStatusCommand command, CancellationToken cancellationToken) public async Task<bool> Handle(SetAwaitingValidationOrderStatusCommand command, CancellationToken cancellationToken)
{ {
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber); var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
if(orderToUpdate == null) if (orderToUpdate == null)
{ {
return false; return false;
} }
orderToUpdate.SetAwaitingValidationStatus(); orderToUpdate.SetAwaitingValidationStatus();
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return true;
} }
} }

View File

@ -30,13 +30,14 @@ namespace Ordering.API.Application.Commands
await Task.Delay(10000, cancellationToken); await Task.Delay(10000, cancellationToken);
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber); var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
if(orderToUpdate == null) if (orderToUpdate == null)
{ {
return false; return false;
} }
orderToUpdate.SetPaidStatus(); orderToUpdate.SetPaidStatus();
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return true;
} }
} }

View File

@ -36,7 +36,8 @@ namespace Ordering.API.Application.Commands
} }
orderToUpdate.SetStockConfirmedStatus(); orderToUpdate.SetStockConfirmedStatus();
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return true;
} }
} }

View File

@ -30,14 +30,15 @@ namespace Ordering.API.Application.Commands
await Task.Delay(10000, cancellationToken); await Task.Delay(10000, cancellationToken);
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber); var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
if(orderToUpdate == null) if (orderToUpdate == null)
{ {
return false; return false;
} }
orderToUpdate.SetCancelledStatusWhenStockIsRejected(command.OrderStockItems); orderToUpdate.SetCancelledStatusWhenStockIsRejected(command.OrderStockItems);
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return true;
} }
} }

View File

@ -33,7 +33,8 @@ namespace Ordering.API.Application.Commands
} }
orderToUpdate.SetShippedStatus(); orderToUpdate.SetShippedStatus();
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return true;
} }
} }

View File

@ -55,8 +55,7 @@ namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
_buyerRepository.Update(buyer) : _buyerRepository.Update(buyer) :
_buyerRepository.Add(buyer); _buyerRepository.Add(buyer);
await _buyerRepository.UnitOfWork await _buyerRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
.SaveEntitiesAsync(cancellationToken);
var orderStatusChangedTosubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(orderStartedEvent.Order.Id, orderStartedEvent.Order.OrderStatus.Name, buyer.Name); var orderStatusChangedTosubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(orderStartedEvent.Order.Id, orderStartedEvent.Order.OrderStatus.Name, buyer.Name);
await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedTosubmittedIntegrationEvent); await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedTosubmittedIntegrationEvent);

View File

@ -7,6 +7,5 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork
public interface IUnitOfWork : IDisposable public interface IUnitOfWork : IDisposable
{ {
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken)); Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken));
Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken));
} }
} }

View File

@ -52,7 +52,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
modelBuilder.ApplyConfiguration(new BuyerEntityTypeConfiguration()); modelBuilder.ApplyConfiguration(new BuyerEntityTypeConfiguration());
} }
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken)) public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{ {
// Dispatch Domain Events collection. // Dispatch Domain Events collection.
// Choices: // Choices:
@ -64,9 +64,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
// After executing this line all the changes (from the Command Handler and Domain Event Handlers) // After executing this line all the changes (from the Command Handler and Domain Event Handlers)
// performed through the DbContext will be committed // performed through the DbContext will be committed
var result = await base.SaveChangesAsync(cancellationToken); return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
return true; public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
// Dispatch Domain Events collection.
// Choices:
// A) Right BEFORE committing data (EF SaveChanges) into the DB will make a single transaction including
// side effects from the domain event handlers which are using the same DbContext with "InstancePerLifetimeScope" or "scoped" lifetime
// B) Right AFTER committing data (EF SaveChanges) into the DB will make multiple transactions.
// You will need to handle eventual consistency and compensatory actions in case of failures in any of the Handlers.
_mediator.DispatchDomainEventsAsync(this).Wait();
// After executing this line all the changes (from the Command Handler and Domain Event Handlers)
// performed through the DbContext will be committed
return base.SaveChanges(acceptAllChangesOnSuccess);
} }
public async Task<IDbContextTransaction> BeginTransactionAsync() public async Task<IDbContextTransaction> BeginTransactionAsync()