DEV Community

Mohammad Karimi
Mohammad Karimi

Posted on

Let's dive a bit deeper into each step of the MediatR architecture

In MediatR architecture, each task is represented by a Request, sent to the corresponding Handler by the framework. However, MediatR acts like a pipeline, not directly passing the Request to the Handler.

MediatR Framework

Instead, it goes through one or more middlewares, called Pipeline Behaviors, in three steps:

Pipeline Behavior in MadiatR

1. Pre-Processing:
In the Pre-Processing step, the incoming Request is handed over to the Pre-processor. This is like the front door of the MediatR pipeline, where the Request is checked, validated, and possibly transformed before proceeding further.
The Pre-processor can perform tasks such as ensuring the Request is in the right format, validating user input, or making any necessary adjustments to the Request data.

2. Next Step:
Assuming the Pre-Processing step succeeds, the pipeline moves to the Next Step. Here, a delegate decides what comes next in the pipeline.
This could be another middleware instance, adding another layer of processing, or the final destination, which is the Handler responsible for carrying out the core logic related to the Request.
Think of it as passing the baton in a relay race; the Request smoothly moves to the next phase of processing.

3. Post-Processing:
After the core logic (handled by the Handler) is executed, the pipeline reaches the Post-Processing step. This is the last stage before the result is sent back to the client.
In Post-Processing, actions like logging the outcome, making final adjustments to the Response, or handling errors are performed.
It's the place where you tie up loose ends, ensuring that everything is in order before the processed data is sent back to the client.

Important Notes:
If there's an issue during the Pre-Processing (e.g., validation failure), the process immediately goes back, bypassing the Next and Post steps. This quick exit ensures that only valid Requests proceed through the entire pipeline.
On the other hand, when everything goes smoothly, the logic is executed, and the Post-Processing step is not extensively used for validation-related tasks. It focuses more on actions needed after the Request is validated and processed.
In essence, the MediatR pipeline is a carefully orchestrated sequence of steps, each playing a vital role in ensuring efficient and organized handling of Requests, from validation to execution and finalization.

Top comments (2)

Collapse
 
raoufebrahimi profile image
Raouf Ebrahimi Nasab

That was awesome. Precise, to the point and utilized.

Collapse
 
liaghatmand profile image
Zahra Liaghatmand

Exploring the MediatR pipeline is crucial for developers aiming to optimize their use of this architecture. The article delves into how requests are handled at various stages, shedding light on the inner workings of the pipeline Behavior in MediatR and its impact on application development
. This in-depth analysis can be particularly beneficial for developers seeking to fine-tune their implementations and leverage the full potential of MediatR.

I appreciate the author's effort in breaking down the complexities of the MediatR architecture, as such articles contribute significantly to the knowledge-sharing within the development community. It's evident that the article aims to empower readers with a deeper understanding.

We also can see how it's used in code in the Repository Made By the Author at this address:
github.com/mohammadKarimi/SwiftLink