Search code examples
c#.netasp.net-mvcgetabstraction

Get Handler Abstraction


I was reading up on how to clean up post functions and the idea seems very useful. I added onto the idea of an IFormHandler by adding a couple more methods and making the class abstract.

public abstract class FormHandler<T>
{
    private readonly UnitOfWork unit;

    public FormHandler(UnitOfWork unit)
    {
        this.unit = unit;
    }

    protected UnitOfWork Unit
    {
        get
        {
            return this.unit;
        }
    }

    public virtual void PreValidation(ModelStateDictionary modelState, T form)
    {

    }

    public abstract void Handle(T form);

    public virtual void OnValidationFailure(T form)
    {

    }
}

This works excellent for post functions and my controllers are alot lighter and easier to understand. I wanted to clean up the GET functions as well. Has anyone already attempted this or have any ideas of how to make the GET functions cleaner?


Solution

  • I like the IQueryProcessor pattern mentioned here. Steven also has a good article on implementing an ICommandHandler interface, similar to what you've done, though with dependency injection rather than an abstract class with inherited methods.