0

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

TheGuest
  • 415
  • 1
  • 4
  • 14

1 Answers1

1

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.

eulerfx
  • 36,769
  • 7
  • 61
  • 83
  • Thank you for your insight. You are right, the reasons was for reuse purposes. But I am wondering if composition is a feasible when modeling events. I can only see composition working if you define your subclasses within your main event class to prevent class sharing between different events. Sharing a common class between multiple events is unwanted since you want your events to be independent. You agree? – TheGuest Feb 06 '13 at 15:01
  • 1
    There are a few reasons why sharing base class is unwanted: makes event contract less clear, changes to base class affect all descendants, may have serialization issues etc. If all of these are anticipated, a base class can be used with caution. From experience, overambitious reuse can become a [fallacy](http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/). – eulerfx Feb 06 '13 at 17:16