简体   繁体   English

DDD事件和抽象基类

[英]DDD Events and abstract base classes

I am currently working on implementing multiple events which share common properties and are basically the same: Templates. 我目前正在致力于实现多个具有相同属性且基本相同的事件:模板。 Our event provider applies several events like SomeTemplateAddedEvent and SomeOtherTemplateAddedEvent. 我们的事件提供程序将应用一些事件,例如SomeTemplateAddedEvent和SomeOtherTemplateAddedEvent。 There could possibly come more variations later, so I was thinking about implementing a base class for each TemplateAddedEvent since they all share common properties. 以后可能会出现更多变化,因此我考虑为每个TemplateAddedEvent实现一个基类,因为它们都共享相同的属性。 But I am doubtful if this is the right way to go, since some people prefer events to be simple classes containing every property instead of having to dig deeper to find out what the event can provide. 但是我怀疑这是否是正确的方法,因为有些人更喜欢事件是包含每个属性的简单类,而不必更深入地了解事件可以提供什么。

I hope someone can shed some light on this subject. 我希望有人能对此有所启发。

Inheritance is normally used for two orthogonal reasons - to reuse functionality and to declare an "is-a" relationship between classes. 通常出于两个正交的原因使用继承-重用功能和声明类之间的“是”关系。 It seems that you're using it for the first reason. 看来您使用它是第一个原因。 This reason is a weaker argument because reuse can also be attained with composition. 这是一个较弱的论据,因为通过组合也可以实现重用。 The question to ask then is whether there exists an "is-a" relationship between the events. 然后要问的问题是事件之间是否存在“是”关系。 Sometimes inheritance among events is desirable, such as when it makes sense to provide a handler for all events deriving from the base class. 有时,需要在事件之间进行继承,例如在为从基类派生的所有事件提供处理程序时有意义。

Overall, I'd caution against inheritance if it is only applied to attain code reuse. 总体而言,如果将继承仅用于实现代码重用,则应避免使用继承。 If it is an appropriate statement about the domain, then it can make sense. 如果这是有关域的适当声明,那么它就很有意义。

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

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