Nomad.Common.UseCases 1.0.0

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

Nomad.Common.UseCases

A high-performance, middleware-capable use case dispatcher for Clean Architecture and DDD applications in .NET. Built for developers who want simplicity, speed, and structure โ€” without magic or reflection.


โœจ Highlights

  • ๐Ÿš€ ~40% faster than MediatR (based on benchmarks)
  • ๐Ÿงฉ First-class middleware support (logging, validation, metrics, etc.)
  • โœ… Clean DI, zero reflection, no scanning magic
  • ๐Ÿงผ Minimal allocations โ€” optimized for hot paths
  • ๐Ÿ”Œ Seamlessly fits into monoliths, microservices, CQRS, or CRUD-style applications

๐Ÿ“ฆ Installation

dotnet add package Nomad.Common.UseCases

๐Ÿ“Œ Execution Flow

Controller โ†’ UseCaseDispatcher โ†’ IUseCase<TRequest, TResponse> โ†’ Result

๐Ÿš€ Quick Start

1. Define a Request & Response:

public class CreateOrderRequest
{
    public Guid CustomerId { get; set; }
    public List<Guid> ProductIds { get; set; }
}

public class CreateOrderResponse
{
    public Guid OrderId { get; set; }
}

2. Implement the Use Case:

public class CreateOrderUseCase : IUseCase<CreateOrderRequest, CreateOrderResponse>
{
    public async Task<CreateOrderResponse> ExecuteAsync(CreateOrderRequest request, CancellationToken cancellationToken = default)
    {
        var newOrderId = Guid.NewGuid();
        return new CreateOrderResponse { OrderId = newOrderId };
    }
}

3. Register Use Cases & Middleware:

builder.Services
    .AddUseCases(typeof(CreateOrderUseCase).Assembly)
    .AddUseCaseMiddleware<LoggingMiddleware>();

4. Dispatch in Controller:

[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
    private readonly IUseCaseDispatcher _dispatcher;

    public OrdersController(IUseCaseDispatcher dispatcher)
    {
        _dispatcher = dispatcher;
    }

    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateOrderRequest request)
    {
        var result = await _dispatcher.ExecuteAsync<CreateOrderRequest, CreateOrderResponse>(request);
        return Ok(result);
    }
}

๐Ÿงฉ Middleware Example

public class LoggingMiddleware : IUseCaseMiddleware
{
    public async Task<TResponse> InvokeAsync<TRequest, TResponse>(
        TRequest request,
        Func<TRequest, object?, CancellationToken, Task<TResponse>> next,
        object? state = null,
        CancellationToken cancellationToken = default)
    {
        Console.WriteLine($"[START] {typeof(TRequest).Name}");
        var result = await next(request, state, cancellationToken);
        Console.WriteLine($"[END] {typeof(TResponse).Name}");
        return result;
    }
}

๐Ÿง  Interfaces

IUseCase

public interface IUseCase<in TRequest, TResponse>
{
    Task<TResponse> ExecuteAsync(TRequest request, CancellationToken cancellationToken = default);
}

IUseCaseMiddleware

public interface IUseCaseMiddleware
{
    Task<TResponse> InvokeAsync<TRequest, TResponse>(
        TRequest request,
        Func<TRequest, object?, CancellationToken, Task<TResponse>> next,
        object? state = null,
        CancellationToken cancellationToken = default);
}

IUseCaseDispatcher

public interface IUseCaseDispatcher
{
    Task<TResponse> ExecuteAsync<TRequest, TResponse>(
        TRequest request,
        CancellationToken cancellationToken = default);
}

๐Ÿ“ˆ Benchmark

Tested on .NET 9 ยท Apple M1 Pro ยท Release build ยท BenchmarkDotNet

Method Mean Allocated
โœ… UseCase Dispatch 52.75 ns 200 B
MediatR Dispatch 84.64 ns 336 B

๐Ÿงช Testing

Use cases can be tested in isolation โ€” no controller or infrastructure needed:

[Fact]
public async Task Should_Create_Order()
{
    var useCase = new CreateOrderUseCase();
    var result = await useCase.ExecuteAsync(new CreateOrderRequest { ... });
    Assert.NotEqual(Guid.Empty, result.OrderId);
}

โœ… Best Practices

  • Keep Use Cases small and focused on business logic
  • Avoid service location โ€” use constructor injection
  • Let controllers just dispatch and return
  • Use middleware for:
    • Logging
    • Validation
    • Retry / metrics
  • Prefer DTOs over domain types at the boundary

๐Ÿ“„ License

MIT License ยท ยฉ Nomad

All feedback and contributions are welcome.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Nomad.Common.UseCases:

Package Downloads
Nomad.Common.MinimalApi

Extensions and tools for ASP.NET Core Minimal APIs

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 206 4/14/2025