Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 7b300ec

Browse files
authored
Merge pull request #1033 from LeeDumond/enhance/cancellation-token-support
provide support for cancellation tokens in command handlers
2 parents 0e8d07d + e4dc903 commit 7b300ec

10 files changed

+13
-13
lines changed

src/Services/Ordering/Ordering.API/Application/Commands/CancelOrderCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task<bool> Handle(CancelOrderCommand command, CancellationToken can
3333
}
3434

3535
orderToUpdate.SetCancelledStatus();
36-
return await _orderRepository.UnitOfWork.SaveEntitiesAsync();
36+
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
3737
}
3838
}
3939

src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public async Task<bool> Handle(CreateOrderCommand message, CancellationToken can
5858
_orderRepository.Add(order);
5959

6060
return await _orderRepository.UnitOfWork
61-
.SaveEntitiesAsync();
61+
.SaveEntitiesAsync(cancellationToken);
6262
}
6363
}
6464

src/Services/Ordering/Ordering.API/Application/Commands/IdentifiedCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async Task<R> Handle(IdentifiedCommand<T, R> message, CancellationToken c
9696
command);
9797

9898
// Send the embeded business command to mediator so it runs its related CommandHandler
99-
var result = await _mediator.Send(command);
99+
var result = await _mediator.Send(command, cancellationToken);
100100

101101
_logger.LogInformation(
102102
"----- Command result: {@Result} - {CommandName} - {IdProperty}: {CommandId} ({@Command})",

src/Services/Ordering/Ordering.API/Application/Commands/SetAwaitingValidationOrderStatusCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task<bool> Handle(SetAwaitingValidationOrderStatusCommand command,
3333
}
3434

3535
orderToUpdate.SetAwaitingValidationStatus();
36-
return await _orderRepository.UnitOfWork.SaveEntitiesAsync();
36+
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
3737
}
3838
}
3939

src/Services/Ordering/Ordering.API/Application/Commands/SetPaidOrderStatusCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public SetPaidOrderStatusCommandHandler(IOrderRepository orderRepository)
2727
public async Task<bool> Handle(SetPaidOrderStatusCommand command, CancellationToken cancellationToken)
2828
{
2929
// Simulate a work time for validating the payment
30-
await Task.Delay(10000);
30+
await Task.Delay(10000, cancellationToken);
3131

3232
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
3333
if(orderToUpdate == null)
@@ -36,7 +36,7 @@ public async Task<bool> Handle(SetPaidOrderStatusCommand command, CancellationTo
3636
}
3737

3838
orderToUpdate.SetPaidStatus();
39-
return await _orderRepository.UnitOfWork.SaveEntitiesAsync();
39+
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
4040
}
4141
}
4242

src/Services/Ordering/Ordering.API/Application/Commands/SetStockConfirmedOrderStatusCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public SetStockConfirmedOrderStatusCommandHandler(IOrderRepository orderReposito
2727
public async Task<bool> Handle(SetStockConfirmedOrderStatusCommand command, CancellationToken cancellationToken)
2828
{
2929
// Simulate a work time for confirming the stock
30-
await Task.Delay(10000);
30+
await Task.Delay(10000, cancellationToken);
3131

3232
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
3333
if(orderToUpdate == null)
@@ -36,7 +36,7 @@ public async Task<bool> Handle(SetStockConfirmedOrderStatusCommand command, Canc
3636
}
3737

3838
orderToUpdate.SetStockConfirmedStatus();
39-
return await _orderRepository.UnitOfWork.SaveEntitiesAsync();
39+
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
4040
}
4141
}
4242

src/Services/Ordering/Ordering.API/Application/Commands/SetStockRejectedOrderStatusCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public SetStockRejectedOrderStatusCommandHandler(IOrderRepository orderRepositor
2727
public async Task<bool> Handle(SetStockRejectedOrderStatusCommand command, CancellationToken cancellationToken)
2828
{
2929
// Simulate a work time for rejecting the stock
30-
await Task.Delay(10000);
30+
await Task.Delay(10000, cancellationToken);
3131

3232
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
3333
if(orderToUpdate == null)
@@ -37,7 +37,7 @@ public async Task<bool> Handle(SetStockRejectedOrderStatusCommand command, Cance
3737

3838
orderToUpdate.SetCancelledStatusWhenStockIsRejected(command.OrderStockItems);
3939

40-
return await _orderRepository.UnitOfWork.SaveEntitiesAsync();
40+
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
4141
}
4242
}
4343

src/Services/Ordering/Ordering.API/Application/Commands/ShipOrderCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task<bool> Handle(ShipOrderCommand command, CancellationToken cance
3333
}
3434

3535
orderToUpdate.SetShippedStatus();
36-
return await _orderRepository.UnitOfWork.SaveEntitiesAsync();
36+
return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
3737
}
3838
}
3939

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStartedEvent/ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task Handle(OrderStartedDomainEvent orderStartedEvent, Cancellation
5656
_buyerRepository.Add(buyer);
5757

5858
await _buyerRepository.UnitOfWork
59-
.SaveEntitiesAsync();
59+
.SaveEntitiesAsync(cancellationToken);
6060

6161
var orderStatusChangedTosubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(orderStartedEvent.Order.Id, orderStartedEvent.Order.OrderStatus.Name, buyer.Name);
6262
await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedTosubmittedIntegrationEvent);

src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
6464

6565
// After executing this line all the changes (from the Command Handler and Domain Event Handlers)
6666
// performed through the DbContext will be committed
67-
var result = await base.SaveChangesAsync();
67+
var result = await base.SaveChangesAsync(cancellationToken);
6868

6969
return true;
7070
}

0 commit comments

Comments
 (0)