How to use MediatR in ASP.Net Core

Take advantage of MediatR and the mediator design pattern to reduce the dependencies between objects in your ASP.Net Core applications

MediatR is an open source project and an implementation of the mediator design pattern. The mediator design pattern controls how a set of objects communicate and helps to reduce the number of dependencies among these objects that you must manage. In the mediator design pattern, objects don’t communicate with one another directly, but through a mediator. This article presents a discussion of how we can use MediatR in ASP.Net Core applications.

Create an ASP.Net Core project in Visual Studio

First off, let’s create an ASP.Net Core project in Visual Studio. Assuming Visual Studio 2017 or Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.Net Core project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.Net Core Web Application” from the list of templates displayed.
  4. Click Next. 
  5. In the “Configure your new project” window that is shown next, specify the name and location for the new project.
  6. Click Create. 
  7. A new window “Create New ASP.Net Core Web Application” is shown next.
  8. Select .Net Core as the runtime and ASP.Net Core 2.2 (or later) from the drop-down list at the top.
  9. Select “Web Application” as the project template.
  10. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here.
  11. Ensure that Authentication is set as “No Authentication” as we won’t be using authentication either.
  12. Click Create. 

This will create a new ASP.Net Core project in Visual Studio. We’ll use this project in the subsequent sections of this article to implement our mediator pattern with MediatR.

To continue reading this article register now