MediatR Pipeline Behaviors

Abdullah SajjadAbdullah Sajjad
3 min read

What are MediatR Pipeline Behaviors?

Pipeline behaviors in MediatR are akin to middleware in ASP.NET Core. They allow you to define a series of steps that a request goes through before reaching its final handler. This can be useful for cross-cutting concerns such as logging, validation, and performance monitoring.

How Pipeline Behaviors Work

When a request is sent through MediatR, it passes through a series of pipeline behaviors before reaching the actual handler. Each behavior can perform some action and then either pass the request to the next behavior in the pipeline or short-circuit the pipeline by returning a response immediately.

Here's a simplified example of how a pipeline behavior might look:

public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;

    public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
    {
        _logger = logger;
    }

    public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
    {
        _logger.LogInformation($"Handling {typeof(TRequest).Name}");
        var response = await next();
        _logger.LogInformation($"Handled {typeof(TResponse).Name}");
        return response;
    }
}

In this example, the LoggingBehavior logs information before and after the request is handled.

Implementing Pipeline Behaviors in CQRS

In a CQRS architecture, you often have commands and queries. Commands are used to change the state of the application, while queries are used to retrieve data. MediatR pipeline behaviors can be used to handle cross-cutting concerns for both commands and queries.

Example: Performance Monitoring Behavior

Performance monitoring is another common requirement. You can create a behavior that logs the time taken to handle each request.

public class PerformanceBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    private readonly ILogger<PerformanceBehavior<TRequest, TResponse>> _logger;
    private readonly Stopwatch _timer;

    public PerformanceBehavior(ILogger<PerformanceBehavior<TRequest, TResponse>> logger)
    {
        _logger = logger;
        _timer = new Stopwatch();
    }

    public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
    {
        _timer.Start();
        var response = await next();
        _timer.Stop();

        if (_timer.ElapsedMilliseconds > 500)
        {
            _logger.LogWarning($"Long Running Request: {typeof(TRequest).Name} ({_timer.ElapsedMilliseconds} milliseconds)");
        }

        return response;
    }
}

In this example, the PerformanceBehavior logs a warning if the request takes longer than 500 milliseconds to handle.

Configuring and Using Pipeline Behaviors

To configure and use these behaviors, you need to register them in your dependency injection container. In an ASP.NET Core application, this is typically done in the Startup.cs file.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMediatR(typeof(Startup));
    services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
    services.AddTransient(typeof(IPipelineBehavior<,>), typeof(PerformanceBehavior<,>));
}

This configuration ensures that the LoggingBehavior and PerformanceBehavior are part of the MediatR pipeline and will be executed for each request.

Conclusion

MediatR pipeline behaviors provide a powerful way to handle cross-cutting concerns in a CQRS architecture. By using behaviors, you can keep your handlers clean and focused on their primary responsibilities while still addressing concerns like validation, logging, and performance monitoring. This leads to a more maintainable and testable codebase, which is essential for building robust applications.

0
Subscribe to my newsletter

Read articles from Abdullah Sajjad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abdullah Sajjad
Abdullah Sajjad

Full Stack - Best in Products’ Dashboards | Next.js - React Native - Typescript - C# - .NET Web APIs - Web3 | Empowering Businesses with Technology - Remote