Resulver.MediatR
9.0.0
dotnet add package Resulver.MediatR --version 9.0.0
NuGet\Install-Package Resulver.MediatR -Version 9.0.0
<PackageReference Include="Resulver.MediatR" Version="9.0.0" />
<PackageVersion Include="Resulver.MediatR" Version="9.0.0" />
<PackageReference Include="Resulver.MediatR" />
paket add Resulver.MediatR --version 9.0.0
#r "nuget: Resulver.MediatR, 9.0.0"
#:package Resulver.MediatR@9.0.0
#addin nuget:?package=Resulver.MediatR&version=9.0.0
#tool nuget:?package=Resulver.MediatR&version=9.0.0
Resulver.MediatR NuGet Package
Table of Contents
Overview
Resulver.MediatR is a lightweight extension for MediatR designed to seamlessly integrate MediatR with the Resulver package. It provides structured request handling with Result objects, ensuring a clean and predictable flow without relying on exceptions.
Before using
Resulver.MediatR, it is recommended to read theResulverdocumentation to understand howResultworks and how to use it effectively.
Installation
To install Resulver.MediatR, use the following command:
dotnet add package Resulver.MediatR
Make sure that both MediatR and Resulver are installed in your project.
Usage
Implementing Request Handlers
Resulver.MediatR provides two interfaces for handling requests:
IResultBaseRequestHandler<TRequest, TResultContent>: For requests that return a specific value within aResult<TResultContent>.IResultBaseRequestHandler<TRequest>: For requests that only return aResultwithout content.
Example 1: Handler with a Specific Return Value
public class SumHandler : IResultBaseRequestHandler<SumRequest, int>
{
public Task<Result<int>> Handle(SumRequest request, CancellationToken cancellationToken)
{
var sum = request.A + request.B;
return Task.FromResult(new Result<int>(sum));
}
}
Example 2: Handler Without a Specific Return Value
public class RemoveUserHandler : IResultBaseRequestHandler<RemoveUserRequest>
{
public Task<Result> Handle(RemoveUserRequest request, CancellationToken cancellationToken)
{
if (request.UserId <= 0)
{
return Task.FromResult(new Result(new UserNotFoundError()));
}
return Task.FromResult(new Result());
}
}
Example Usage
public class SumRequest : IResultBaseRequest<int>
{
public int A { get; }
public int B { get; }
public SumRequest(int a, int b)
{
A = a;
B = b;
}
}
public class MyController
{
private readonly IMediator _mediator;
public MyController(IMediator mediator)
{
_mediator = mediator;
}
public async Task<IActionResult> CalculateSum(int a, int b)
{
var request = new SumRequest(a, b);
var result = await _mediator.Send(request);
if (result.IsFailure)
{
return BadRequest(result.Errors);
}
return Ok(result.Content);
}
}
Result and Error Handling
With Resulver.MediatR, you can handle errors using ResultError objects:
public class UserNotFoundError : ResultError
{
public UserNotFoundError() : base("User not found") { }
}
public class InvalidUserIdError : ResultError
{
public InvalidUserIdError() : base("Invalid User ID") { }
}
Handling Multiple Errors
public Task<Result> Handle(RemoveUserRequest request, CancellationToken cancellationToken)
{
var errors = new List<ResultError>();
if (request.UserId <= 0)
{
errors.Add(new InvalidUserIdError());
}
if (!errors.Any())
{
//....
return Task.FromResult(new Result("User removed"));
}
return Task.FromResult(new Result(errors));
}
Real-World Example
public class AuthenticateRequest : IResultBaseRequest<User>
{
public string Username { get; }
public string Password { get; }
public AuthenticateRequest(string username, string password)
{
Username = username;
Password = password;
}
}
public class AuthenticateHandler : IResultBaseRequestHandler<AuthenticateRequest, User>
{
private readonly IUserRepository _userRepository;
public AuthenticateHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public Task<Result<User>> Handle(AuthenticateRequest request, CancellationToken cancellationToken)
{
var user = _userRepository.Find(request.Username, request.Password);
if (user == null)
{
return Task.FromResult(new UserNotFoundError());
}
return Task.FromResult(new Result<User>(user,"Authentication successful"));
}
}
Best Practices
- Always check
IsSuccessorIsFailurebefore accessing the result content or errors. - Use meaningful messages and errors to provide clear debugging information.
- Avoid mixing exceptions with
Result, keeping control flow predictable. - Handle multiple errors when necessary, leveraging the flexibility of
Result. - Keep handlers simple, ensuring they focus on a single responsibility.
By following these guidelines, you can effectively use Resulver.MediatR to write cleaner, more maintainable code.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 9.0.0 | 398 | 3/7/2025 |