dimanche 10 novembre 2019

Is there any pattern for executing all the registered implementations of an interface?

I have a Exception handling middleware in asp.net core app, which uses the below IExceptionHandler to handle exceptions

public interface IExceptionHandler<T> where T:Exception
{
    ProblemDetails HandleException(T exception);
}

I want to register different kinds of exception handlers such as

// Handles Sql based exceptions
public class SqlExceptionHandler: IExceptionHandler<SqlException>
{
    public ProblemDetails Handle(SqlException exception);
}

// Handles File based exceptions 
public class IOExceptionHandler : IExceptionHandler<IOException>
{
    public ProblemDetails Handle(IOException exception);
}

// Handles all the exceptions that are not handled by any handlers
public class ExceptionHandler : IExceptionHandler<Exception>
{
    public ProblemDetails Handle(Exception exception)
}

when the exception middleware gets triggered, i want the exception handler to be triggered automatically based on the type, if the exception is unhandled by any of these then want to have ExceptionHandler handle the exception.

Is it possible to trigger the exception handler instance automatically based on type? Also, I want this IExceptionHandler to be extensible such that in future if i want to handle any other type of Exception, I just register the implementation

Aucun commentaire:

Enregistrer un commentaire