SharpDispatch 1.0.1

dotnet add package SharpDispatch --version 1.0.1
                    
NuGet\Install-Package SharpDispatch -Version 1.0.1
                    
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="SharpDispatch" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpDispatch" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="SharpDispatch" />
                    
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 SharpDispatch --version 1.0.1
                    
#r "nuget: SharpDispatch, 1.0.1"
                    
#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 SharpDispatch@1.0.1
                    
#: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=SharpDispatch&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=SharpDispatch&version=1.0.1
                    
Install as a Cake Tool

<div align="center"> <img src="SharpDispatch.jpg" alt="SharpDispatch Logo" width="150" /> </div>

SharpDispatch

Blazingly fast CQRS command dispatching for .NET 10
Zero allocations in hot paths โ€ข Native AOT ready โ€ข Zero external dependencies

SharpDispatch is a lightweight, high-performance CQRS command dispatching library for .NET 10.

๐Ÿš€ Key Features

  • โšก Sub-microsecond dispatch โ€” ~10โ€“15ns latency, zero allocations for hot paths
  • ๐ŸŽฏ Native AOT ready โ€” Full ahead-of-time compilation support
  • ๐Ÿ“ฆ Zero dependencies โ€” Only Microsoft.Extensions.DependencyInjection.Abstractions
  • ๐Ÿ”’ Type-safe โ€” Compile-time handler registration and exhaustive dispatch
  • ๐Ÿงช Test-friendly โ€” In-memory dispatcher for fast unit tests
  • โ™ป๏ธ Zero-copy ready โ€” Pass ReadOnlySpan<T> and Memory<T> through commands

โœจ What's Included

Type Purpose
ICommand Marker interface for commands
ICommandHandler<T> Handler contract
ICommandDispatcher Dispatcher abstraction
CommandDispatchResult Stack-allocated result (record struct)
ServiceProviderCommandDispatcher DI-based dispatcher (default)
InMemoryCommandDispatcher In-memory dispatcher for tests
OptimizedCommandDispatcher High-performance FrozenDictionary-based dispatcher
CommandDispatcherBuilder AOT-safe fluent builder

๐ŸŽฏ Quick Start

1. Define a Command & Handler

using SharpDispatch;

// Command
public class CreateOrderCommand : ICommand
{
    public required string OrderId { get; init; }
    public required decimal Amount { get; init; }
}

// Handler
public class CreateOrderCommandHandler : ICommandHandler<CreateOrderCommand>
{
    public async Task<CommandDispatchResult> HandleAsync(
        CreateOrderCommand command, 
        CancellationToken cancellationToken)
    {
        // Handle command...
        return CommandDispatchResult.Ok($"Order {command.OrderId} created");
    }
}

2. Register & Dispatch

using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddCommandHandler<CreateOrderCommand, CreateOrderCommandHandler>();
services.AddCommandDispatcher();  // or AddOptimizedCommandDispatcher()

var provider = services.BuildServiceProvider();
var dispatcher = provider.GetRequiredService<ICommandDispatcher>();

var result = await dispatcher.DispatchAsync(
    new CreateOrderCommand { OrderId = "ORD-001", Amount = 99.99m }
);

๐Ÿ”ฅ Three Dispatcher Options

1. ServiceProviderCommandDispatcher (Default)

Resolves handlers from DI on each call.

  • Best for: Most applications, mixed handler lifetimes
  • Overhead: ~50โ€“200ns per dispatch
services.AddCommandDispatcher();

Pre-built typed delegates + FrozenDictionary = zero-allocation dispatch.

  • Best for: APIs, high-throughput workloads
  • Hot path: ~10โ€“15ns for singleton handlers
services.AddOptimizedCommandDispatcher(cfg =>
{
    cfg.AddHandler<CreateOrderCommand, CreateOrderCommandHandler>();
});

3. InMemoryCommandDispatcher (For Testing)

In-memory handler registry without DI.

var dispatcher = new InMemoryCommandDispatcher();
dispatcher.RegisterHandler(new CreateOrderCommandHandler());

๐ŸŒ Native AOT Ready

Zero reflection โ€ข Fully verifiable โ€ข Ready for PublishAot.

services.AddOptimizedCommandDispatcher(cfg =>
{
    cfg.AddHandler<CreateOrderCommand, CreateOrderCommandHandler>();
});

No MakeGenericType, no Activator.CreateInstance โ€” just explicit generic instantiation the AOT compiler can see.

๐Ÿงช Perfect for Testing

[Fact]
public async Task ShouldCreateOrder()
{
    var dispatcher = new InMemoryCommandDispatcher();
    dispatcher.RegisterHandler(new CreateOrderCommandHandler());
    
    var result = await dispatcher.DispatchAsync(
        new CreateOrderCommand { OrderId = "ORD-001", Amount = 99.99m }
    );
    
    Assert.True(result.Success);
}

๐Ÿ“š Learn More

For comprehensive documentation, benchmarks, and advanced patterns, visit the GitHub repository.


<div align="center">

Questions? Open an issue or discussion on GitHub.

Made with โค๏ธ for .NET developers who care about performance.

</div>

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.1 47 4/14/2026
1.0.0 38 4/14/2026

Second release of SharpDispatch with SourceLink integration - high-performance CQRS command dispatching for .NET 10.