Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD generic vs. specific domain events

I'm looking at domain events, specifically at 2 possibilities:

A. Using "generic" events like that:

public class OrderStateChangedEvent : IEvent
{
  public Guid OrderId { get; }
  public OrderState NewState { get; }
}

then the consumer would figure out the new state and do something. Since I'm using Rx this would be not that hard (and way better than a switch/case):

var subscription = DomainEvents
     .AsObservable<OrderStateChangedEvent>()
     .Where(e => e.NewState == OrderState.OrderCompleted)
     .Subscribe(OnOrderCompleted);

B. Using specific events:

public class OrderCompletedEvent : IEvent
{
  public Guid OrderId { get; }
}

this would lead to way more event classes, which on one hand may get too many, on the other hand event class names contain language and this may be a plus point.

Do you have any recommendations? I'm not experienced with domain events and can't really make a qualified decision here (both seem not to have any major drawbacks)

like image 246
Lev Avatar asked Oct 18 '22 22:10

Lev


2 Answers

This would boil down to whether the "generic" events results in any behaviour. If it merely carries an interesting value as something like a "category" may then a generic event will suffice.

However, if there is any specific behaviour attached to the event and it, therefore, has any specific meaning in the system I would definitely suggest going for a more explicit approach.

like image 196
Eben Roux Avatar answered Oct 21 '22 15:10

Eben Roux


As a general rule of thumb, using the switch statement is often considered a code smell in OO languages.

http://c2.com/cgi/wiki?SwitchStatementsSmell

B. is more explicit and also makes the code even more concise, if I understand it correctly:

var subscription = DomainEvents
   .AsObservable<OrderCompletedEvent>()
   .Subscribe(OnOrderCompleted);
like image 34
Alexander Langer Avatar answered Oct 21 '22 17:10

Alexander Langer