Last week I made my first open source release, a mediator implementation in C# called Appeaser. I got insipred by a series of blog posts by Jimmy Bogard about putting your controllers on a diet, which is pretty much in tune with previous attempts I have made of removing repositories in favour of a command/query based pattern. I then ended up by putting my command and query execution logic in the controllers which never felt quite clean and required me to copy paste abstract base controller logic from project to project.
Enter the mediator pattern. I could now end up with code like this in my controllers (validation handling taken care of elsewhere):
public class PersonController : Controller
{
//...
public void ActionResult Index(PagingParameters paging, SortingParameters sorting)
{
var viewModel = _mediator.Request(new UserListQuery(paging, sorting));
return View(viewModel);
}
public void ActionResult Edit(Guid id)
{
var viewModel = _mediator.Request(new UserFormQuery(id));
return View(viewModel);
}
//...
}
Read more about using the appeaser mediator on it’s github page. There’s also a version out on nuget as well as a pre-release version with async support.