CQRS - Domain Exceptions for Events for Exceptional Scenarios

I am wondering if it is preferable to post events rather than throw exceptions from aggregates. Let's say I have a domain in which there is a requirement that students of a certain level can sign up for a sport. If I call EnrollForSports on a student who does not meet the criteria, should the unit exclude or post the event, especially if other agents or process managers are interested in the result of the process?

If an event is published, does this not mean that to handle the event, when we replay, an appropriate internal event handler will be needed, even if the event does not change the state of the aggregate?

If an exception is selected, how will the other parties be notified? Can a command handler catch an exception and then raise an event? Can events be raised from command handlers?

+6
source share
2 answers

In principle, a command must be either valid, or executable, or invalid and not executable. The idea of ​​spawning events leaves you somewhere in between, and the feedback from whoever sends the command is ambiguous and delayed. This is also unnecessary complexity in your domain. If you throw an exception, it will be immediately passed to the client who sent the command.

+5
source

This question cannot be answered in general. There is no β€œbest practice," it really depends on many factors.

However, this topic has recently been discussed on the DDD / CQRS mailing list . Read it and you will probably find interesting and useful points there.

Oh, and here's a blog post on the topic: Business Mistakes Are Just Common Events

+3
source

Source: https://habr.com/ru/post/951237/


All Articles