The Liaison solution - no more "thin" controllers

About

Liaison is a framework created over the top of MediatR and ASP.NET Core. It was released as version 1.0.0 on NuGet

Knowledge of MediatR is recommended to help you understand this post. Read more about MediatR here.

How can this help your applications? Behaviour Driven Design is about putting your users first in your system. We can design systems in such a way that they effectively model your company and its processes. Current .NET frameworks tend to push developers to more technical separation of concerns rather than a behaviour based separation of concerns which maps to the business.

By making our application structure and language more user centric and business centric the conversations developers have with product owners suddenly becomes more efficient, more effective, and more productive.

 

The problem with "thin" controllers

Using MediatR on its own creates these "thin" controllers, which meant you were just using ASP.NET Core to route from socket to MediatR. This just seemed pointless to me for many reasons including unnecessary overhead, hidden complexity and abstractions.

Also, MediatR is based on Behaviour Driven Design, so separating your requests means you can separate your actions. ASP.NET Controllers tie your actions together, where it seems Microsoft have gone for a technical separation of concerns (controllers, models, view models, data layer...).

 

Unintended side effect 

Having all the actions in a Controller has a knock on effect on each action being executed which require all the services passed in the Controller constructor.

So if you had a User controller, and someone "Signed Up", an email is usually sent. If someone wanted to get all the Users, the communication service will be called into existence... for what?

See example:

It is easy to see here that every time the GetAll action is called, it will bring into scope all the other interfaces defined in the class constructor. This is unnecessary coupling.

Here is the example updated using MediatR and having thin controllers:

The Liaison solution

We’ve written a library which combines the core of ASP.NET Core (Middleware) and the MediatR library in a way that allows HTTP requests to get automatically bound to MediatR requests and are automatically fired off. These can be immutable, thanks to the Newtonsoft Json library for being able to bind JSON data to constructor arguments.

The setup is easy.

You should install Liaison with NuGet:

 $ Install-Package Liaison

Or via the .NET Core command line interface:

 $ dotnet add package Liaison

First, we need to configure services, so add:

services.AddLiaison();

to the ConfigureServices method in Startup.cs.

Then we simply need to configure the routes and requests in the Configure method in Startup.cs

It is super simple to setup and use - and that is the point. No more thin controllers. No more MVC framework on top. All of the requests can be immutable so that the data can’t be changed throughout MediatR or ASP.NET Core pipelines.

MediatR pipeline still applies, because we’re just wiring up HTTP Requests directly to MediatR Requests. Everything else still applies as normal.

One thing to note: because we’re negating the use of ASP.NET Core MVC/Web API framework, not everything is as extensively implemented yet. But, nothing stops you using those frameworks as well as Liaison to achieve every feature you need.