I was reading some examples how to handle concurrency in DDD. One example is in this repository: https://github.com/kgrzybek/efcore-concurrency-handling/tree/master/src/OptimisticConcurrency/DDD.EF.OptimisticConcurrency
And the code in which is increased the version is this:
public async Task<IActionResult> AddOrderLine(AddOrderLineRequest request)
{
var orderId = Guid.Parse("33d4201c-4a8e-40a2-ae1d-50bc64097085");
var order = await _ordersContext.Orders.FindAsync(orderId);
Thread.Sleep(3000);
order.AddOrderLine(request.ProductCode);
var domainEvents = DomainEventsHelper.GetAllDomainEvents(order);
if (domainEvents.Any())
{
order.IncreaseVersion();
}
await _ordersContext.SaveChangesAsync();
return Ok();
}
My doubt is if it is really needed to add an event to tell the order is modified. If the order line couldn't be added, I would get an exception because of valiation, and if I don't get an exception it is beacuse the line could be added, so I could increase the version directly, without the need to check if I get some event.
Or perhaps if I want to ensure that the line is added, I could return a bool an avoid the needed to implement the events for this case.
Or another solution, if the domain is conscious about versions and concurrency, why don't increase the version in the method AddOrderLine() of the Order entity if all went well? Why to delegate to the consumer of the domain in the application layer?
Which is the benefit to add an event in this case?
Thanks.