Ninct.CommandRouter 1.0.2

dotnet add package Ninct.CommandRouter --version 1.0.2
                    
NuGet\Install-Package Ninct.CommandRouter -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ninct.CommandRouter" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ninct.CommandRouter" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Ninct.CommandRouter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ninct.CommandRouter --version 1.0.2
                    
#r "nuget: Ninct.CommandRouter, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ninct.CommandRouter@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ninct.CommandRouter&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Ninct.CommandRouter&version=1.0.2
                    
Install as a Cake Tool

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> and IRequestHandler<TRequest, TResponse> for clean request/response flow
  • IPipelineBehavior<TRequest, TResponse> support for logging, validation, etc.
  • INotification and INotificationHandler<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

  1. Define a Request
public class PingRequest : IRequest<string>
{
    public string Message { get; init; }
} 
  1. Implement a Request Handler
public class PingRequestHandler : IRequestHandler<PingRequest, string>
{
    public Task<string> Handle(PingRequest request, CancellationToken cancellationToken)
    {
        return Task.FromResult($"Pong: {request.Message}");
    }
}
  1. Register Services
builder.Services.AddHandlersFromAppDomain();
  1. 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)

  1. Define a Notification
public class UserRegisteredNotification : INotification
{
    public string Email { get; set; }
}
  1. 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;
    }
}
  1. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.2 97 3/21/2026
1.0.1 206 5/20/2025