Browse Source

remove SaveEntities and move the functionality overriding the SaveChanges method

feature/refactor_remove_saveEntities_and_migrate_functionality_to_saveChanges
ericuss 4 years ago
parent
commit
2d826edc88
10 changed files with 38 additions and 21 deletions
  1. +2
    -1
      src/Services/Ordering/Ordering.API/Application/Commands/CancelOrderCommandHandler.cs
  2. +2
    -2
      src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs
  3. +4
    -3
      src/Services/Ordering/Ordering.API/Application/Commands/SetAwaitingValidationOrderStatusCommandHandler.cs
  4. +4
    -3
      src/Services/Ordering/Ordering.API/Application/Commands/SetPaidOrderStatusCommandHandler.cs
  5. +2
    -1
      src/Services/Ordering/Ordering.API/Application/Commands/SetStockConfirmedOrderStatusCommandHandler.cs
  6. +5
    -4
      src/Services/Ordering/Ordering.API/Application/Commands/SetStockRejectedOrderStatusCommandHandler.cs
  7. +2
    -1
      src/Services/Ordering/Ordering.API/Application/Commands/ShipOrderCommandHandler.cs
  8. +1
    -2
      src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStartedEvent/ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler.cs
  9. +0
    -1
      src/Services/Ordering/Ordering.Domain/SeedWork/IUnitOfWork.cs
  10. +16
    -3
      src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs

+ 2
- 1
src/Services/Ordering/Ordering.API/Application/Commands/CancelOrderCommandHandler.cs View File

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


+ 2
- 2
src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs View File

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


+ 4
- 3
src/Services/Ordering/Ordering.API/Application/Commands/SetAwaitingValidationOrderStatusCommandHandler.cs View File

@ -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;
}
}


+ 4
- 3
src/Services/Ordering/Ordering.API/Application/Commands/SetPaidOrderStatusCommandHandler.cs View File

@ -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;
}
}


+ 2
- 1
src/Services/Ordering/Ordering.API/Application/Commands/SetStockConfirmedOrderStatusCommandHandler.cs View File

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


+ 5
- 4
src/Services/Ordering/Ordering.API/Application/Commands/SetStockRejectedOrderStatusCommandHandler.cs View File

@ -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;
}
}


+ 2
- 1
src/Services/Ordering/Ordering.API/Application/Commands/ShipOrderCommandHandler.cs View File

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


+ 1
- 2
src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStartedEvent/ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler.cs View File

@ -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);


+ 0
- 1
src/Services/Ordering/Ordering.Domain/SeedWork/IUnitOfWork.cs View File

@ -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));
}
}

+ 16
- 3
src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs View File

@ -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()


Loading…
Cancel
Save