Mohammad Neamul Islam
Senior Software Engineer
Bangla
English
Hindi
  • ASP .NET CORE
  • SQL & MYSQL & PostgreSQL & LinQ
  • Angular
  • C#, ASP.Net MVC , Entityframework
  • GIT, SVN
  • WebAPI, SignalR, Autofac, Hangfire
  • Amazon EC2, Amazon S3, Amazon RDS, Amazon IAM, Amazon VPC

Asp >net Core Mediator Pattern

ASP .NET CORE

Mediator pattern is a Behavioral design pattern.

Mediator pattern is used to reduce communication complexity between multiple objects or classes.This pattern provide a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loos coupling.

 

The main intent of the pattern is to have an object that encapsulates interaction between a set of objects.It promotes loose coupling by keeping objects from referring to each other explicitly.

I allows us to “reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object. ”

The Mediator Design Pattern reduces the communication complexity between multiple objects. This design pattern provides a mediator object, which will be responsible for handling all the communication complexities between different objects.

The Mediator Design Pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object. This pattern is used to centralize complex communications and control between related objects in a system. The Mediator object acts as the communication center for all objects. That means when an object needs to communicate with another object, it does not call the other object directly. Instead, it calls the mediator object, and it is the responsibility of the mediator object to route the message to the destination object.

 

Example to Understand Mediator Design Pattern:

Let us understand the Mediator Design Pattern in C# with an Example. Please have a look at the following diagram. As shown in the diagram below, we have four objects (Object A, Object B, Object C, and Object D). And these four objects want to communicate with each other. Suppose Object A wants to communicate with Object B, then Object A should know the reference of Object B, and using that reference, Object A can call the method of Object B. Similarly if Object B wants to send some message to Object C. It should know the reference of Object C, and using that reference, it will call the methods of Object C and send the message.

The couplings between the objects are more, i.e., tight coupling. A lot of object knows each other. Now, in the above image, four objects are there. In real-time, you may have thousands of objects, and those thousands of objects want to communicate with each other. Then, writing code and maintaining code is very difficult.

How do we Reduce the Coupling Between the Objects?

Using the Mediator Design Pattern, we can reduce the coupling between objects. To understand this, please have a look at the following diagram. As shown in the image below, here we introduce the Mediator object. Each object has to send messages to the mediator object. The mediator object here will receive the message from each object and route that message to the destination object. So, the mediator object will take care of handling the messages. So, in this scenario, we can use the Mediator Design Pattern.

  • Publish Date:
    Dec. 21, 2023
  • Author:
  • Category:
    ASP .NET CORE

Similar posts

blog post
Rate limiting middleware in ASP.NET Core
.NET 7 provides a rate limiter middleware for setting rate limiting policies. Web apps can configure these policies in Program.cs and then use them with API endpoints.
Read more
blog post
Why is ASP.NET Core 11x faster than Node.js?
There are 1000 reasons why, but in short: .NET 7 got about 1000 performance-impacting PRs across the runtime, core libraries, ASP.NET Core, Windows Forms, Entity Framework and beyond. Here's a quick summary of what got improved: 𝗝𝗜𝗧 𝗖𝗼𝗺𝗽𝗶𝗹𝗲𝗿: Superior assembly code dumping, inlining, and vectorization. 𝗚𝗮𝗿𝗯𝗮𝗴𝗲 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗼𝗿: Enhanced memory management with reduced fragmentation. 𝗡𝗮𝘁𝗶𝘃𝗲 𝗔𝗢𝗧: Improved startup time & reduced memory usage.
Read more
blog post
.𝐍𝐄𝐓 8 𝐃𝐚𝐭𝐚𝐀𝐧𝐧𝐨𝐭𝐚𝐭𝐢𝐨𝐧
.𝐍𝐄𝐓 8 𝐃𝐚𝐭𝐚𝐀𝐧𝐧𝐨𝐭𝐚𝐭𝐢𝐨𝐧 𝐚𝐭𝐭𝐫𝐢𝐛𝐮𝐭𝐞𝐬. Data annotations are basically used for model validation. These are the attributes that can be applied to the members of the class to specify some validation rules. In this blog, we will discuss one of the enhancements in data annotation for the class member validation in .NET 8.
Read more
© 2024 Mohammad Neamul Islam

Mohammad Neamul Islam