0

In C# I have a scenario where in at least 2 places different domain events are raised and I want a single hander to handle them with the same code (other listeners may perform the event specific code). With the handlers using the following pattern;

public class SomeHandler : IHandler<SomeEvent>
{
   public SomeHandler()
   {
      //whatever init code
   }

   public void Handle(SomeArgs args)
   {
       //Common code
   }
}

So what is the best way to handle more than one Event with the same Handler? Thanks

stan4th
  • 710
  • 1
  • 6
  • 19
  • Silly decision, keep things simple. I've never seen such thing as a handler handling different types of event. (beside using properties of an event base class if it has one) – aybe Apr 10 '14 at 23:48
  • This isn't really DDD .. its more EDA - perhaps re-tagging is required? – Simon Whitehead Apr 11 '14 at 00:45
  • The reasoning is that there is common code to perform in both events and I don't want to duplicate it (DRY). – stan4th Apr 11 '14 at 08:43

1 Answers1

3

IHandler<SomeEvent> is an interface so perhaps you can implement multiple ones:

public class SomeHandler : IHandler<SomeEvent>, IHandler<SomeOtherEvent>
{
   public SomeHandler()
   {
      //whatever init code
   }

   public void Handle(SomeArgs args)
   {
       //Common code
   }

   public void Handle(SomeOtherArgs args)
   {
       //Common code
   }
}
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
  • This solves it - thanks. (I'll consider whether I've got some higher design decisions prompted by Aybe). – stan4th Apr 11 '14 at 08:45