Search code examples
c#domain-driven-designdomain-events

How to handle two or more domainevents in c#?


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


Solution

  • 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
       }
    }