TS.MediatR
9.0.6
dotnet add package TS.MediatR --version 9.0.6
NuGet\Install-Package TS.MediatR -Version 9.0.6
<PackageReference Include="TS.MediatR" Version="9.0.6" />
<PackageVersion Include="TS.MediatR" Version="9.0.6" />
<PackageReference Include="TS.MediatR" />
paket add TS.MediatR --version 9.0.6
#r "nuget: TS.MediatR, 9.0.6"
#:package TS.MediatR@9.0.6
#addin nuget:?package=TS.MediatR&version=9.0.6
#tool nuget:?package=TS.MediatR&version=9.0.6
TS.MediatR
TS.MediatR is a lightweight and flexible CQRS/Mediator library for .NET. It supports IRequest
, INotification
, IPipelineBehavior
, and works seamlessly with Dependency Injection.
🔧 Installation
Install via NuGet:
dotnet add package TS.MediatR
🚀 Getting Started
using TS.MediatR;
services.AddMediatR(options =>
{
options.AddRegisterAssemblies(typeof(Program).Assembly);
options.AddOpenBehavior(typeof(LoggingBehavior<,>)); //with response
options.AddOpenBehavior(typeof(ValidationBehavior<>)); //no response
});
🧩 IRequest / IRequestHandler
Define a request and its corresponding handler:
With Response
public class GetUserQuery : IRequest<UserDto>
{
public int Id { get; set; }
}
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto>
{
public Task<UserDto> Handle(GetUserQuery request, CancellationToken cancellationToken)
{
var user = new UserDto { Id = request.Id, Name = "Jhon Doe" };
return Task.FromResult(user);
}
}
No Response
public class CreateUserCommand : IRequest
{
public string Name { get; set; }
}
public class GetUserQueryHandler : IRequestHandler<CreateUserCommand>
{
public Task Handle(GetUserQuery request, CancellationToken cancellationToken)
{
var user = new UserDto { Name = request.Name };
return Task.FromResult(user);
}
}
📤 Sending with ISender
Use ISender
to send the request:
With Response
var result = await sender.Send(new GetUserQuery { Id = 1 });
Console.WriteLine(result.Name);
No Response
await sender.Send(new CreateUserCommand { Name = "Jhon Doe" });
🔁 Pipeline Behavior
Use IPipelineBehavior
for cross-cutting concerns like logging, validation, etc.
With Response
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public async Task<TResponse> Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
{
Console.WriteLine($"Request received: {typeof(TRequest).Name}");
var response = await next();
Console.WriteLine($"Response returned: {typeof(TResponse).Name}");
return response;
}
}
No Response
public class ValidationBehavior<TRequest> : IPipelineBehavior<TRequest>
where TRequest : IRequest
{
public async Task Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
{
Console.WriteLine($"Request received: {typeof(TRequest).Name}");
await next();
Console.WriteLine($"Response returned: {typeof(TResponse).Name}");
return response;
}
}
📣 Notification Handling
Define a notification and its handler:
public class ProductCreatedEvent : INotification
{
public Guid Id { get; set; }
public ProductCreatedEvent(Guid id)
{
Id = id;
}
}
public class SendEmailHandler : INotificationHandler<ProductCreatedEvent>
{
public Task Handle(ProductCreatedEvent notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Id sent: {notification.Id}");
return Task.CompletedTask;
}
}
Publish a notification:
await sender.Publish(new ProductCreatedEvent(product.Id));
📦 Features
- ✅ Request/Response (
IRequest
,IRequest<TResponse>
) - ✅ Notifications (
INotification
) - ✅ Pipeline behaviors (
IPipelineBehavior
) - ✅ Dependency Injection ready
- ✅ Scoped resolution and handler chaining
- ✅ Fully async support
📁 License
Licensed under the MIT License.
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
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.