Mecesoft.MediatR
1.0.3
dotnet add package Mecesoft.MediatR --version 1.0.3
NuGet\Install-Package Mecesoft.MediatR -Version 1.0.3
<PackageReference Include="Mecesoft.MediatR" Version="1.0.3" />
<PackageVersion Include="Mecesoft.MediatR" Version="1.0.3" />
<PackageReference Include="Mecesoft.MediatR" />
paket add Mecesoft.MediatR --version 1.0.3
#r "nuget: Mecesoft.MediatR, 1.0.3"
#:package Mecesoft.MediatR@1.0.3
#addin nuget:?package=Mecesoft.MediatR&version=1.0.3
#tool nuget:?package=Mecesoft.MediatR&version=1.0.3
📦 Mecesoft.MediatR
A lightweight, high-performance MediatR implementation for .NET 9.0, designed with simplicity and efficiency in mind.
✨ Features
- � High Performance: Optimized reflection with caching for minimal runtime overhead.
- 🛡️ Scope Safe: Correctly handles dependency injection scopes, ensuring transaction boundaries and scoped services (like
DbContext) work as expected. - 🧩 Modular: Supports
IRequest,IRequestHandler,INotification, andIPipelineBehavior. - � Safe Scanning: Robust assembly scanning that gracefully handles missing dependencies.
- ✅ Pipeline Support: Easily add cross-cutting concerns like Validation, Logging, or Caching.
🧱 Installation
dotnet add package Mecesoft.MediatR
🔧 Usage
1. Register Services
In your Program.cs or startup configuration:
using Mecesoft.MediatR;
builder.Services.AddMediatR(cfg =>
{
// Register handlers from the assembly containing 'Program'
cfg.AddRegisterAssemblies(typeof(Program).Assembly);
// Register pipeline behaviors (optional)
cfg.AddOpenBehavior(typeof(ValidationBehavior<>));
cfg.AddOpenBehavior(typeof(LoggingBehavior<>));
});
2. Create Request & Handler
// Define a Request (Command/Query)
public record GetUserByIdQuery(Guid Id) : IRequest<UserDto>;
// Implement the Handler
public class GetUserByIdHandler : IRequestHandler<GetUserByIdQuery, UserDto>
{
private readonly IUserRepository _repository;
public GetUserByIdHandler(IUserRepository repository)
{
_repository = repository;
}
public async Task<UserDto> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
{
var user = await _repository.GetByIdAsync(request.Id);
return user.ToDto();
}
}
3. Send Request
Inject ISender into your Controllers or Services:
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly ISender _sender;
public UsersController(ISender sender)
{
_sender = sender;
}
[HttpGet("{id}")]
public async Task<IActionResult> Get(Guid id)
{
var result = await _sender.Send(new GetUserByIdQuery(id));
return Ok(result);
}
}
⚠️ Important: Middleware & Singleton Services
If you are using ISender inside a Middleware or any Singleton service, you MUST NOT inject it via the constructor. Instead, inject it into the method (Method Injection) to ensure it uses the correct scope.
❌ Wrong (Captive Dependency):
public class MyMiddleware
{
private readonly ISender _sender;
public MyMiddleware(RequestDelegate next, ISender sender) // Error!
{
_sender = sender;
}
}
✅ Correct (Method Injection):
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context, ISender sender) // Correct!
{
await sender.Send(new MyCommand());
await _next(context);
}
}
🏷 License
MIT License – © Mecesoft
| 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. |
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Security Update