Writing decoupled code with MediatR: The Mediator Pattern
Tags: csharp, dotnet, dotnetcore, aspnetcore
Published: 2021-01-09 10:09:01

---
title: Writing decoupled code with MediatR: The Mediator Pattern
published: true
description: MediatR is an implementation of the mediator pattern. It is a behavioural software design pattern that helps you to build simpler code by making all components communicate via a "mediator" object, instead of directly with each other. This helps the code to remain highly decoupled and reduces the number of complex dependencies between objects.
tags: csharp,dotnet,dotnetcore,aspnetcore
cover_image: https://cdn.hashnode.com/res/hashnode/image/upload/v1610186322099/Ip2kGTSOT.jpeg
canonical_url: https://samwalpole.com/writing-decoupled-code-with-mediatr-the-mediator-pattern
---

I recently wrote a blog post introducing some of my favourite NuGet packages: [My Top 4 NuGet Packages for ASP.NET Core](https://samwalpole.com/my-top-4-nuget-packages-for-aspnet-core). In that post, I briefly introduced a package called MediatR. Today, I will dedicate this post to revisiting MediatR in further detail.

# What is MediatR?
MediatR is an implementation of the mediator pattern. It is a behavioural software design pattern that helps you to build simpler code by making all components communicate via a "mediator" object, instead of directly with each other. This helps the code to remain highly decoupled and reduces the number of complex dependencies between objects.

A good real-world example of the mediator pattern is the air-traffic control (ATC) tower at airports. If every plane had to directly communicate with every other plane, it would be chaos. Instead, they all report to the ATC tower, and the tower decides how to relay those messages to other aircraft. In this, scenario the ATC tower is the mediator object.

Using the MediatR package, one sends some data as an object to the mediator object. Depending on the type of data that is sent to the mediator object, it decides which other objects/services to call. MediatR handles two forms of message:
- Requests - can only send messages to one other object/service. Can return the result from that object/service to the original caller.
- Notifications - can send messages to multiple objects/services. Cannot receive any data back.

<a href="https://www.amazon.co.uk/Design-patterns-elements-reusable-object-oriented/dp/0201633612/ref=as_li_ss_il?dchild=1&keywords=design+patterns+book&qid=1610186634&sr=8-1&linkCode=li2&tag=samwalpole-21&linkId=afd240ef5ca986debecf54543033da82&language=en_GB" target="_blank"><img border="0" src="//ws-eu.amazon-adsystem.com/widgets/q?_encoding=UTF8&ASIN=0201633612&Format=_SL160_&ID=AsinImage&MarketPlace=GB&ServiceVersion=20070822&WS=1&tag=samwalpole-21&language=en_GB" ></a><img src="https://ir-uk.amazon-adsystem.com/e/ir?t=samwalpole-21&language=en_GB&l=li2&o=2&a=0201633612" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />

# Setting up MediatR
To get MediatR, install the `MediatR` package from NuGet. If you are using ASP NET Core, you should also install the `MediatR.Extensions.Microsoft.DependencyInjection`, which provides an easy way to register all of the MediaR services:
``` csharp
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  // other services

  services.AddMediatR(typeof(Startup));
}
```
If you are using a different method of dependency injection, please [check the wiki](https://github.com/jbogard/MediatR/wiki) on how to configure MediatR for your container.

# Sending Requests
To send a request, you need to create two objects: a request and a request handler.

The request object should implement the `IRequest` or `IRequest<TResponse>` interfaces, depending on whether or not you want to return any data. The request object should consist of any data that you want to send to the handler.
``` csharp
public class AdditionRequest : IRequest<int>
{
    public int A { get; set; }
    public int B { get; set; }
}
```

The request handler should implement the `IRequestHandler<TRequest>` or `IRequestHandler<TRequest, TResponse>` interfaces, where TRequest is the request object that you just created.
``` csharp
public class AdditionRequestHandler : IRequestHandler<AdditionRequest, int>
{
    public Task<int> Handle(AdditionRequest request, CancellationToken cancellationToken)
    {
        var result = request.A + request.B;

        return Task.FromResult(result);
    }
}
```

To then send the request via MediatR, one must use the Send method on the `IMediator` instance, passing in an instance of `AdditionRequest`.
``` csharp
public class MyCalculator
{
    private readonly IMediator _mediator;

    public MyCalculator(IMediator mediator)
    {
        _mediator = mediator;
    }

    public async Task<int> Add(int a, int b)
    {
        var request = new AdditionRequest { A = a, B = b };
        var result = await _mediator.Send(request);

        return result;
    }
}
```

Note that if you choose not to return anything from a request handler, MediatR actually has a special 'nothing' value that you must return instead, called Unit.
``` csharp
public class MyRequest : IRequest 
{
    // some properties
}

public class MyRequestHandler : IRequestHandler<MyRequest>
{
    public Task<Unit> Handle(MyRequest request, CancellationToken cancellationToken)
    {
        // do stuff

        return Task.FromResult(Unit.Value);
    }
}
```

# Sending Notifications
Sending notifications is very similar to sending requests, in that a notification object and a notification handler object must be created. The difference here is that multiple notification handler objects can be created, which will all be called when a notification is sent to MediatR.
``` csharp
public class MyNotification : INotification
{
    // some properties
}

public class MyNotificationHandler1 : INotificationHandler<MyNotification>
{
    public Task Handle(MyNotification notification, CancellationToken cancellationToken)
    {
        // do stuff
        
        return Task.CompletedTask;
    }
}

public class MyNotificationHandler2 : INotificationHandler<MyNotification>
{
    public Task Handle(MyNotification notification, CancellationToken cancellationToken)
    {
        // do stuff

        return Task.CompletedTask;
    }
}
```

Then to actually send the notification, call the the Publish method on the `IMediator` instance, passing in an instance of your notification object. When Publish is called, both MyNotificationHandler1 and MyNotificationHandler2 will be executed.
``` csharp
public class MyService
{
    private readonly IMediator _mediator;

    public MyService(IMediator mediator)
    {
        _mediator = mediator;
    }

    public async Task Execute()
    {
        var notification = new MyNotification
        {
            // initialise
        };

        await _mediator.Publish(notification);
    }
}
```

<a href="https://www.amazon.co.uk/8-0-NET-Core-3-0-Cross-Platform/dp/1788478126/ref=as_li_ss_il?ie=UTF8&aaxitk=U83blMNYhHflTNwD4MDoCw&hsa_cr_id=6216680800702&pd_rd_plhdr=t&pd_rd_r=43c8a5d2-8f4b-4a7c-ab5a-948b598557e1&pd_rd_w=Z28gS&pd_rd_wg=HLO3N&ref_=sbx_be_s_sparkle_mcd_asin_0_img&linkCode=li2&tag=samwalpole-21&linkId=4f8bcbb8c19d5f3c86419ae1099614c6&language=en_GB" target="_blank"><img border="0" src="//ws-eu.amazon-adsystem.com/widgets/q?_encoding=UTF8&ASIN=1788478126&Format=_SL160_&ID=AsinImage&MarketPlace=GB&ServiceVersion=20070822&WS=1&tag=samwalpole-21&language=en_GB" ></a><img src="https://ir-uk.amazon-adsystem.com/e/ir?t=samwalpole-21&language=en_GB&l=li2&o=2&a=1788478126" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />

# Pipeline Behaviours
Pipeline behaviours are a type of middleware that get executed before and after a request (only supports requests, not notifications). They can be useful for a number of different tasks, such as logging, error handling, request validation etc.

These behaviours work in the same way that you would expect for middleware: you can chain multiple behaviours together, each behaviour gets executed in turn until it reaches the end of the chain, then the actual request handler is executed, then the result of this is passed back up the chain.

For example, a LoggingBehaviour could write to the application log before and after each request:
``` csharp
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;

    public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
    {
        _logger = logger;
    }

    public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
    {
        _logger.LogInformation($"Handling {typeof(TRequest).Name}");

        // go to the next behaviour in the chain/to the request handler
        var response = await next();

        _logger.LogInformation($"Handled {typeof(TResponse).Name}");

        return response;
    }
}
```

In ASP NET Core, to register each PipelineBehaviour with the IoC container, add the following line to the ConfigureServices method of the the Startup class:
``` csharp
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehaviour<,>));
```
For other containers, [check the wiki](https://github.com/jbogard/MediatR/wiki/Behaviors).

# Conclusion
In this article, I have introduced the mediator pattern and the NuGet package MediatR, which itself is an implementation of the mediator pattern. I have showed you how to send both requests (one-to-one) and notifications (one-to-many), as well as how to write middleware (pipeline behaviours) that gets executed before/after each request.

["Ten++ Ways to Make Money as a Developer" eBook](https://gumroad.com/a/633205875)

I post mostly about full stack .NET and Vue web development. To make sure that you don't miss out on any posts, please follow this blog and [subscribe to my newsletter](https://samwalpole.com/). If you found this post helpful, please like it and share it. You can also [find me on Twitter](https://twitter.com/dr_sam_walpole).