How to use the mediator design pattern in C#

Ruhollah Jafari
4 min readSep 9, 2021

Take advantage of the mediator design pattern to promote loose coupling and simplify the coding of object interactions

Design patterns are used to solve common design problems and reduce the complexities in our code. The mediator pattern is a behavioral design pattern that promotes loose coupling between objects and helps to organize the code for inter-object communications.

This article presents a discussion of the mediator design pattern and how it can be implemented using C#.

What is the mediator design pattern?

Imagine an application in which there are many objects that are communicating with each other. The mediator design pattern is useful when the number of objects grows so large that it becomes difficult to maintain the references to the objects. The mediator is essentially an object that encapsulates how one or more objects interact with each other. The mediator design pattern controls how these objects communicate, and helps to reduce the number of dependencies among them that you have to manage.

In the mediator design pattern, the objects don’t communicate with one another directly but through the mediator. When an object needs to communicate with another object or a set of objects, it transmits the message to the mediator. The mediator then transmits the message to each receiver object in a form that is understandable to it.

By eliminating the direct communication between objects, the mediator design pattern promotes loose coupling. The other benefit of using the mediator design pattern is that it improves code readability and maintainability.

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

Note that the mediator design pattern differs from the facade design pattern. The mediator pattern facilitates how a set of objects interact, while the facade pattern simply provides a unified interface to a set of interfaces in the application. Thus the mediator pattern is a behavior pattern that deals with object behavior, the facade pattern is a structural pattern that deals with object composition.

Implementing the mediator design pattern in C#

But enough of the concepts — let’s get into some code. The participants in this design are the mediator, the concrete mediator, and one or more participant types. While the mediator is responsible for defining the interface for communicating with the participants, the concrete mediator, as the name suggests, implements the mediator interface and has knowledge of the participants. Note that the participant type is sometimes called a colleague. So in some implementations, you have colleague and concrete colleague types.

This is the source of demo project :

First of all we should add package mediatr with this command

dotnet add package MediatR --version 9.0.0

As you can see in the published project in Github after adding model and data access layer we add query and command and in query using record because record are like class but records just redonly and we can’t rewrite them , this record impelements : MediatR.IRequest<T<Response>>

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

Now we talking about handler : every query will have one handler for example GetPersonListHandler first of all impelement : IRequestHandler <GetPersonListQuery , List<PersonModel>>

the first generic that pass is what you going to handle and second generic is what you expect what the output is going to be

Important point in this two cs file is different between :

IRequest is for your command or query and IRequestHandler going to handle that query or that command

after impelement IRequestHandler it’s going to add handle method that pass two parameters consist of request mehod and cancellation Token Because every thing is asynchronous in mediator

after inject DataAccess layer the handler return the expected value after Task completed using Task.FromResult(_data.GetPeople())

So we have a handler and we have a query and pretty much done in our demo library for first use of mediator and then we can call the query from controller or blazor as you can see in the source project .

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

Thanks for your attention , please let me know what do you think about the story .

--

--

Ruhollah Jafari

I am a Full Stack .Net developer with good experience in developing web applications using Asp.Net Frame Work And Core, Mvc , WebApi , JavaScript ,Sql Server ,