Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Should I use the Symfony EventDispatcher for my domain events?

I wonder if I should use the Symfony EventDispatcher Component for handling Domain Events in my DDD application.

To me it makes little sense to reiplement an event dispatcher from scratch when a well tested component is available. But on the other hand, the Symfony component makes all events extend from a base Event class. Won't this tie my domain events to the EventDispatcher component?

Also, if I do use this component, should I raise domain events on the application-wide event_dispatcher service or should I keep Symfony's application events and my domain events separate (i.e. create a new EventDispatcher)?

like image 401
Sander Marechal Avatar asked Jul 30 '14 11:07

Sander Marechal


1 Answers

I have wondered why there is no EventInterface.

This: https://github.com/symfony/symfony/issues/9539 indicates that because Event is a value object then no interface is needed. I confess that I don't quite follow the argument (at least with respect to coupling) but have not researched further. Might be a good DDD question.

In my case, I created a DomainEventInterface which is implemented in DomainEvent which in turn extends Event. I use DomainEvent as my base event class. This should make re-factoring fairly easy if I even need to switch dispatchers. And, at least in my own mind, avoids coupling the Domain directly to the Event component.

I make my own DomainDispatcher instance mostly to avoid tying my domain to the actual Symfony framework.

Note also that the event listener interface has changed in S2.4. The Event object will be simplified in 3.0.

like image 119
Cerad Avatar answered Nov 15 '22 00:11

Cerad