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
<PackageReference Include="Nomad.Common.UseCases" Version="1.0.0" />
<PackageVersion Include="Nomad.Common.UseCases" Version="1.0.0" />
<PackageReference Include="Nomad.Common.UseCases" />
paket add Nomad.Common.UseCases --version 1.0.0
#r "nuget: Nomad.Common.UseCases, 1.0.0"
#:package Nomad.Common.UseCases@1.0.0
#addin nuget:?package=Nomad.Common.UseCases&version=1.0.0
#tool nuget:?package=Nomad.Common.UseCases&version=1.0.0
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 | 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 (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 |