Creating and using controllers with MediatR
In this recipe, you will learn another way to work with MVC controllers without services and repositories.
Note
This recipe could be applied to the WebAPI controller. We could add that it's logical to mix MVC and WebAPI practices in the same controller. Now, there's no difference between them.
Getting ready
We created an empty web application with VS 2017.
How to do it...
- First, let's add the MediatR Dependency Injection package in
project.json
. It will include the MediatR 4.0.0 package:
"MediatR.Extensions.Microsoft.DependencyInjection": "4.0.0"
We will also need the AutoMapper
package to map Business models
to ViewModels
. We will talk about AutoMapper
in more detail in Chapter 13, Views, Models, and ViewModels:
"AutoMapper.Extensions.Microsoft.DependencyInjection": "3.2.0"
- Next, let's add some configuration in
Startup.cs
:
public class Startup { public void ConfigureServices(IServiceCollection services) { var connection = @"Data Source=MyServer;Initial...