Within an application, it is occasionally necessary for one object or component to connect with another. There are no problems if the components are small. Real-time applications, on the other hand, consist of numerous components, all of which must communicate with one another. The more components there are in the application, the more complex it becomes. In this post, we'll look at how to use the Mediator approach to solve this issue and how to include it into a.NET Web API.
Let's understand the real-time problem using the following example.
We have four components.
- Component A
- Component B
- Component C
- Component D
These components need to communicate with each other. For example, if Component A wants to communicate with Component B, Component A must have a reference to Component B and use that reference to call Component B's methods. Similarly, if any component wants to send a message to another component, it must know the reference of the other component and use it to call its methods.
The objects are tightly coupled, meaning many objects know each other. In the example, we have only four objects. However, in real-world applications, you might have hundreds of objects that need to communicate with each other. It will be very difficult and will increase the complexity of the application.
A mediator design pattern is a behavioral design pattern that restricts the direct communication between entities and forces them to interact through a mediator object. It reduces the communication complexity between the components.
In the Mediator pattern, direct communication between components is restricted. Instead, all components communicate indirectly through a dedicated mediator object. The Mediator object serves as the communication hub for all components. Each component interacts with the mediator object, which then routes the messages to the appropriate destination component.
Let's consider the above example and re-design the application using the Mediator pattern as described below.
Chat Application is a perfect example of the Mediator design pattern. In this application, there are users and groups. Users can join groups and share their messages within the group. When 'Person A' shares a message in the chat group, it is sent to all members who have joined the group. In this scenario, the chat group acts as the Mediator.
Components involved in Mediator Pattern
- Component: Components are various classes with their own business logic. Each component references a mediator, as defined by the mediator interface.
- Mediator: The Mediator interface defines methods for communication with components, mainly featuring a notification method.
- Concrete Mediator: Concrete Mediators manage the interactions between various components.
Output
How to Implement Mediator Pattern in .NET Web API?
MediatR is a widely used library that simplifies the implementation of the Mediator pattern in .NET applications.
Step 1. Install the MediatR package.
You can do this through the NuGet Package Manager or by running the following command in the Package Manager Console.
Step 2. Define the Request and Response Models.
This class represents the request model for sending a message. It implements the IRequest<string> interface from MediatR, indicating that it expects a response of type string.
Step 3. Create the Handler.
This class handles the processing of the SendMessageCommand. It implements the IRequestHandler<SendMessageCommand, string> interface, which indicates it handles SendMessageCommand requests and returns a string response. The Handle method processes the request and returns a formatted string containing the sender and the message.
Step 4. Configure MediatR in the Startup.
In the Startup class, MediatR is added to the service collection using services.AddMediatR(typeof(Startup).Assembly). This registers all MediatR handlers found in the specified assembly.
Step 5. Send a Message Using MediatR.
This controller handles incoming HTTP requests for sending messages. It injects an instance of IMediator via its constructor. The SendMessage action method receives a SendMessageCommand object as its parameter, sends the command using MediatR, and returns the result as an HTTP response.
Flow
- When a POST request is made to the sending endpoint of the ChatController, the SendMessageCommand is created with the sender's name and message content.
- The SendMessageCommand is sent to MediatR using _mediator.Send(command).
- MediatR locates the appropriate handler (SendMessageHandler) for the command.
- The SendMessageHandler processes the command by formatting the sender's name and message into a string and returns this string as the response.
- The ChatController returns the formatted string as the HTTP response.
This approach centralizes communication through MediatR, reducing
direct dependencies between components and making the code more modular
and easier to maintain.
ASP.NET Core 9.0 Hosting Recommendation
At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.
0 comments:
Post a Comment