Advertisement
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Apr 24, 2022

MediatR with .NET 6.0

Автор:
HungryWolf
Источник:
Просмотров:
1571

What is MediatR? Why do we need it? And How to use it?

Mediator Pattern - The mediator pattern ensures that objects do not interact directly instead of through a mediator. It reduces coupling between objects which makes it easy to maintain the code. Mediator pattern unified the service interfaces around a common design, one request in, one response out.

MediatR is a simple, unambitious mediator implementation in .NET.

Let’s now dive deeper into the working of a simple web API with MediatR. Complete code for the same can be found here.

1. Create a new web API project.

OpenAPI support makes it easy to test the API.

2. Create a folder with these three classes.


Solution Hierarchy

2.1 ApiRequestModel: This class represents a request for API.


ApiRequestModel

IRequest<> It represents a request with a response.

2.2 ApiResponseModel : This class represents a response of API.


ApiResponseModel

2.3 ApiHandler: And this class keeps the logical part.


ApiResponseModel

IRequesthandler<> binds request and response with handler class. The logic of API is kept in the Handle method.

3. Now let’s check the controller. I wrote a base controller that would be inherited by other controllers.


ApiControllerBase

ISender is responsible for communication between different mediator-linked objects. It sends a request through a mediator pipeline to be handled by a single handler.

In this code, HomeController is inheriting the base controller. ApiController communicates with ApiHandler based on ApiRequestModel and ApiResponseModel pair.


ApiController

You can test this API using swagger (OpenAPI).

From the above example, you can see a separation of request, response, logic, and communication using MediatR. And if you want to move to CQRS (Command and Query Responsibility Segregation) pattern, the mediator makes the transition seamless.

Похожее
Sep 5, 2023
Author: Edson Moisinho
Simplifying Data Transport in C#.In modern C# development, data transport objects (DTOs) play a crucial role in exchanging information between different layers of an application, such as between a client and a server, and traditionally, developers have used classes to...
Aug 23, 2022
Author: Luis Rodrigues
Suppose we are building a web api that contains a route to send notification messages to other systems.For security matters, before sending any notification message, we need to provide some credentials to these systems to they accept our messages.However, these...
Feb 7, 2021
Author: Manikanta Pattigulla
OverviewValidation is a very crucial part of Web API implementation. Regardless of what kind of Web API you are building, the bottom line is validating a request before processing it. The common thing I will do in validation is null...
Aug 9, 2021
Author: MBARK T3STO
If you’re anything like me, you’ve looked for ways of making your applications faster. In this article, I’ll show you a somewhat unknown class in the .NET framework that makes lazy creation of objects easy to do and thread safe.The...
Написать сообщение
Почта
Имя
*Сообщение


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban