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)
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.
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);
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