remove SaveEntities and move the functionality overriding the SaveChanges method

This commit is contained in:
ericuss 2020-04-07 17:52:21 +02:00
Родитель c948bee891
Коммит 2d826edc88
10 изменённых файлов: 38 добавлений и 21 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -10,7 +10,7 @@ namespace Ordering.API.Application.Commands
{
// Regular CommandHandler
public class SetAwaitingValidationOrderStatusCommandHandler : IRequestHandler<SetAwaitingValidationOrderStatusCommand, bool>
{
{
private readonly IOrderRepository _orderRepository;
public SetAwaitingValidationOrderStatusCommandHandler(IOrderRepository orderRepository)
@ -27,13 +27,14 @@ namespace Ordering.API.Application.Commands
public async Task<bool> Handle(SetAwaitingValidationOrderStatusCommand command, CancellationToken cancellationToken)
{
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
if(orderToUpdate == null)
if (orderToUpdate == null)
{
return false;
}
orderToUpdate.SetAwaitingValidationStatus();
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return true;
}
}

Просмотреть файл

@ -10,7 +10,7 @@ namespace Ordering.API.Application.Commands
{
// Regular CommandHandler
public class SetPaidOrderStatusCommandHandler : IRequestHandler<SetPaidOrderStatusCommand, bool>
{
{
private readonly IOrderRepository _orderRepository;
public SetPaidOrderStatusCommandHandler(IOrderRepository orderRepository)
@ -30,13 +30,14 @@ namespace Ordering.API.Application.Commands
await Task.Delay(10000, cancellationToken);
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
if(orderToUpdate == null)
if (orderToUpdate == null)
{
return false;
}
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();
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return true;
}
}

Просмотреть файл

@ -10,7 +10,7 @@ namespace Ordering.API.Application.Commands
{
// Regular CommandHandler
public class SetStockRejectedOrderStatusCommandHandler : IRequestHandler<SetStockRejectedOrderStatusCommand, bool>
{
{
private readonly IOrderRepository _orderRepository;
public SetStockRejectedOrderStatusCommandHandler(IOrderRepository orderRepository)
@ -30,14 +30,15 @@ namespace Ordering.API.Application.Commands
await Task.Delay(10000, cancellationToken);
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
if(orderToUpdate == null)
if (orderToUpdate == null)
{
return false;
}
}
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();
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.Add(buyer);
await _buyerRepository.UnitOfWork
.SaveEntitiesAsync(cancellationToken);
await _buyerRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
var orderStatusChangedTosubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(orderStartedEvent.Order.Id, orderStartedEvent.Order.OrderStatus.Name, buyer.Name);
await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedTosubmittedIntegrationEvent);

Просмотреть файл

@ -7,6 +7,5 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork
public interface IUnitOfWork : IDisposable
{
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());
}
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
// Dispatch Domain Events collection.
// 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)
// 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()