I am confused with when to use domain events and when to use double dispatch. Udi encourages us to use domain events to maintain encapsulation of model and away from anemic design, but in my opinion domain events can only be used after the operation has been performed or ended abruptly, not at the time when the operation is in progress.
For example: while solving some business logic I need to access the repository, and then based on the value, I can raise domain events. But the question is how the model can access repository at the time of solving business logic, here in my opinion we could use double dispatch.
Please tell me, are the above statements are valid or not?
Use domain events to explicitly implement side effects of changes within your domain. In other words, and using DDD terminology, use domain events to explicitly implement side effects across multiple aggregates.
Double dispatch is a pattern you can use in C# to control how communication flows between two objects. A frequent use of the pattern is to pass "this" to a function on another class, allowing that class to communicate back to or manipulate the calling object instance.
Integration events are used for bringing domain state in sync across multiple microservices or external systems. This functionality is done by publishing integration events outside the microservice.
There's been quite much talking about this.
I personally aggree with Szymon Pobiega:
My own rule of thumb is use Domain Events (as desscribed by Udi) as a preferred way of IoC when interaction can be made one-way. If it can't, I use double dispatch.
For eg -: while solving some business logic i need to access repository, and then based on value, i can raise domain events.
Your aggregate root should contain everything You need to perform operation. There shouldn't be a need for repository while doing something. Aggregate root draws transactional boundaries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With