c# Factory Pattern Generic Exception Handler using dot net core My task was to create exception handler on some system that has multiple types of database and big business behavior.

At first, I had done a switch/case that handled the exceptions by type using the Object.GetType() method. After some additional thought, I realized that the types of exceptions are endless --- which meant that, every time a new error is thrown, I would have to add more logic to the switch/case. That will make the switch/case so ugly.

So, I have done it by this way. I need your help if i can improve it more or do I need to change the implementation? Also, where are the weaknesses of this implementation?

IExceptionHandlerFactory

private readonly IHttpContextAccessor HttpContext;
private readonly ILogManager LogManager;
private Dictionary&lt;ExceptionsType, Func&lt;ExceptionHandler&gt;&gt; EntityTypeMapper = 
    new Dictionary&lt;ExceptionsType, Func&lt;ExceptionHandler&gt;&gt;();

public ExceptionHandlerFactory(IHttpContextAccessor httpContext, ILogManager logManager)
{
    HttpContext = httpContext;
    LogManager = logManager;
    EntityTypeMapper.Add(ExceptionsType.EFException, () =&gt; 
        { return new EFExceptionHandler(HttpContext, LogManager); });
    EntityTypeMapper.Add(ExceptionsType.Exception, () =&gt;
        { return new GenericExceptionHandler(HttpContext, LogManager); });
    EntityTypeMapper.Add(ExceptionsType.OracleException, () =&gt; 
        { return new OracleExceptionHandler(HttpContext, LogManager); });
}

public ExceptionHandler GetExceptionHandlerBasedOnType(ExceptionsType type)
{
    return EntityTypeMapper[type]();
}


ExceptionHandler

public abstract class ExceptionHandler : Exception
{
    private readonly ILogManager LogManager;

    public ExceptionsType ExceptionsType { get; set; }

    public abstract void Handle(Exception exception);

    public ExceptionHandler(ILogManager logManager)
    {
        LogManager = logManager;
    }

    public void AddLogForException&lt;T&gt;(
        T exception, 
        IHttpContextAccessor httpContext, 
        string description) 
        where T : Exception
    {

        LogManager.AddLog()
    }

}


GenericExceptionHandler

public class GenericExceptionHandler : ExceptionHandler
{
    private readonly IHttpContextAccessor httpContext;

    public ILogManager LogManager { get; }

    public GenericExceptionHandler(
        IHttpContextAccessor httpContext, 
        ILogManager logManager) : base(logManager)
    {
        this.httpContext = httpContext;
    }

    public override void Handle(Exception exception)
    {
        AddLogForException(exception, httpContext, string.Empty);
    }
}
Well, AddLogForException isn't doing anything with it's parameters, perhaps make it virtual or do something with them?

You could also add a rule engine stating if a exception is registered with a specific action then do that, else do default.

Your registration could be a dictionary or thread safe dictionary or dictionary  

say your class would have a backing field like this:

Dictionary&lt;exception, Action&lt;Exception,IHttpContextAccessor,string&gt;&gt; dict = new Dictionary&lt;exception, Action&lt;Exception,IHttpContextAccessor,string&gt;&gt;();


and you can register items like this 

public void Register(Exception ex, Action&lt;Exception,IHttpContextAccessor,string&gt; del)
{
   lock(this)
   {
      dict.Add(ex, del);
   }
}

public override AddLogForException&lt;T&gt;(
        T exception, 
        IHttpContextAccessor httpContext, 
        string description) 
        where T : Exception
    {

       if(dict.TryGetValue(exception, out var delegate)
       {
           delegate.DynamicInvoke(new object[]{exception,httpContext,description});
       }
       else 
           base.AddLogForException(exception,httpContext,description)
    }


This registration is similat to a swich statement only this is a way to manage the options in the consuming project and leaves room for updates as you go on a and adapt it on a case by case as long as your exceptions are granular enough that you can add a "rule" on them. 

I have no access to VS now but I guess it will compile just fine. if you have an issue just send me comment and I will have a look for you.