-1

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.

2
  • Just a note. This is only someone else code made public for whatever reason. The author can not prove credit or authority about the subject, so it might or might not to be DDD "done right". I'm not saying it's wrong either, it's just the interpretation of DDD made by a stranger on the internet. In real-world applications, the implementation of DDD varies (dramatically I dare to say) from project to project. So, be sceptic about everything you read on the Internet.
    – Laiv
    Commented Oct 18, 2022 at 14:40
  • 2
    I do not see an event being "added" or raised. The way I interpret this is the existence of events is checked to see if anyone cares about orders being added. If not, there is no need to bump up the version. It looks like an optimization rather than anything else. But it is hard to tell from an out of context snippet. Commented Oct 19, 2022 at 5:17

1 Answer 1

1

Fire and forget is extremely loose coupling. Achieve it and you don’t care what happens next. That’s something else’s problem.

Creating events, event handlers, and exception handlers is how you set up that something else. Done right the only thing you have to know here is that the time to do it is now.

To learn more read up on: Tell don’t ask.=

4
  • well, my doubt is why not to increase version directly? it is the same or similar than fire and forget, because if I don't get exception, I increase. The domain is not coupled too and it has not need to implement events. Commented Oct 17, 2022 at 16:15
  • 1
    It's your choice to micro manage or not. Pull every detail toward you and obsess over it or push it behind a useful abstraction so you can forget it. Commented Oct 17, 2022 at 16:24
  • @candid_orange: And that’s just the details you know about today. Next week I add a view that displays the number of order lines. It observes the “order line added” event because it needs to update the number of lines. The micromanager doesn’t know about this view because it doesn’t even exist yet! So the display shows “four orders” after you added the fifth.
    – gnasher729
    Commented Oct 19, 2022 at 7:38
  • @gnasher729 true. It’s nice when you can add new features by adding new files rather than editing old tested ones. Commented Oct 19, 2022 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.