Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use domain event vs double dispatch

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?

like image 780
kamal Avatar asked Feb 01 '11 20:02

kamal


People also ask

When to use domain events?

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.

What is Double dispatch in C#?

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.

What is integration event?

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.


1 Answers

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.

like image 193
Arnis Lapsa Avatar answered Oct 16 '22 00:10

Arnis Lapsa