简体   繁体   English

DDD:我应该将Symfony EventDispatcher用于我的域事件吗?

[英]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. 我想知道是否应该使用Symfony EventDispatcher Component来处理我的DDD应用程序中的域事件。

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. 但另一方面,Symfony组件使所有事件都从基类Event类扩展。 Won't this tie my domain events to the EventDispatcher component? 这不会将我的域事件绑定到EventDispatcher组件吗?

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 (ie create a new EventDispatcher)? 此外,如果我使用此组件,我应该在应用程序范围的event_dispatcher服务上引发域事件,还是应该将Symfony的应用程序事件和我的域事件分开(即创建一个新的EventDispatcher)?

I have wondered why there is no EventInterface. 我想知道为什么没有EventInterface。

This: https://github.com/symfony/symfony/issues/9539 indicates that because Event is a value object then no interface is needed. 这: https//github.com/symfony/symfony/issues/9539表示因为Event是一个值对象,所以不需要任何接口。 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. 可能是一个很好的DDD问题。

In my case, I created a DomainEventInterface which is implemented in DomainEvent which in turn extends Event. 在我的例子中,我创建了一个DomainEventInterface,它在DomainEvent中实现,而DomainEvent又扩展了Event。 I use DomainEvent as my base event class. 我使用DomainEvent作为我的基本事件类。 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. 并且,至少在我看来,避免将Domain直接耦合到Event组件。

I make my own DomainDispatcher instance mostly to avoid tying my domain to the actual Symfony framework. 我主要创建自己的DomainDispatcher实例,以避免将我的域绑定到实际的Symfony框架。

Note also that the event listener interface has changed in S2.4. 另请注意,事件侦听器接口在S2.4中已更改。 The Event object will be simplified in 3.0. Event对象将在3.0中简化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM