remove SaveEntities and move the functionality overriding the SaveChanges method
This commit is contained in:
parent
c948bee891
commit
2d826edc88
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,8 +57,8 @@
|
|||||||
|
|
||||||
_orderRepository.Add(order);
|
_orderRepository.Add(order);
|
||||||
|
|
||||||
return await _orderRepository.UnitOfWork
|
await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
|
||||||
.SaveEntitiesAsync(cancellationToken);
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
@ -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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user