Ninct.CommandRouter
1.0.2
dotnet add package Ninct.CommandRouter --version 1.0.2
NuGet\Install-Package Ninct.CommandRouter -Version 1.0.2
<PackageReference Include="Ninct.CommandRouter" Version="1.0.2" />
<PackageVersion Include="Ninct.CommandRouter" Version="1.0.2" />
<PackageReference Include="Ninct.CommandRouter" />
paket add Ninct.CommandRouter --version 1.0.2
#r "nuget: Ninct.CommandRouter, 1.0.2"
#:package Ninct.CommandRouter@1.0.2
#addin nuget:?package=Ninct.CommandRouter&version=1.0.2
#tool nuget:?package=Ninct.CommandRouter&version=1.0.2
CommandRouter
CommandRouter is a lightweight CQRS-style mediator pattern implementation for .NET projects. It provides a simple way to decouple business logic using commands, queries, and pipeline behaviors — all with automatic handler registration.
Note: This library was developed primarily for my personal use.
Anyone is free to copy, modify, and use it in their own projects without restrictions.
✨ Features
- ✅
IRequest<TResponse>andIRequestHandler<TRequest, TResponse>for clean request/response flow - ✅
IPipelineBehavior<TRequest, TResponse>support for logging, validation, etc. - ✅
INotificationandINotificationHandler<TNotification>for pub/sub-style event broadcasting - ✅ Zero external dependencies
- ✅ Supports automatic handler discovery via assembly scanning
📦 Installation
Add the project to your solution as a reference or include it via NuGet (coming soon).
dotnet add package Ninct.CommandRouter
🚀 Getting Started
- Define a Request
public class PingRequest : IRequest<string>
{
public string Message { get; init; }
}
- Implement a Request Handler
public class PingRequestHandler : IRequestHandler<PingRequest, string>
{
public Task<string> Handle(PingRequest request, CancellationToken cancellationToken)
{
return Task.FromResult($"Pong: {request.Message}");
}
}
- Register Services
builder.Services.AddHandlersFromAppDomain();
- Send a Request
public class MyService
{
private readonly CommandPusher _pusher;
public MyService(CommandPusher pusher)
{
_pusher = pusher;
}
public async Task<string> DoWork()
{
var result = await _pusher.Send(new PingRequest { Message = "Hello" });
return result;
}
}
📢 Notifications (Optional)
- Define a Notification
public class UserRegisteredNotification : INotification
{
public string Email { get; set; }
}
- Handle the Notification
public class SendWelcomeEmailHandler : INotificationHandler<UserRegisteredNotification>
{
public Task Handle(UserRegisteredNotification notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Sending email to {notification.Email}");
return Task.CompletedTask;
}
}
- Publish the Notification
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
Console.WriteLine($"Handling {typeof(TRequest).Name}");
var response = await next();
Console.WriteLine($"Handled {typeof(TResponse).Name}");
return response;
}
}
🧩 Pipeline Behaviors
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
Console.WriteLine($"Handling {typeof(TRequest).Name}");
var response = await next();
Console.WriteLine($"Handled {typeof(TResponse).Name}");
return response;
}
}
📄 License
This project is open source and available under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.