Hlibz.UseCases 2.0.0-preview.2

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

Hlibz.UseCases icon

Hlibz.UseCases

NuGet CI

Use cases for .NET apps with more than one client: HTTP endpoints, MCP tools, gRPC services and jobs all call the same handler, and every rule lives in the use case, validation included, written as DataAnnotations on the use case record.

dotnet add package Hlibz.UseCases
public sealed record CreateCityCommand(
    [Required, MaxLength(60)] string Name,
    [Range(1, 50_000_000)] int Population) : IUseCase;

[Error("atlas_1002", ResultStatus.Conflict, "The country already has a city by that name.")]
public sealed record CityNameTaken(int CityId) : Error;

public union CreateCityError(CityNameTaken);

public interface ICreateCityCommandHandler : IUseCaseHandler<CreateCityCommand, CityDto, CreateCityError>;

internal sealed class CreateCityCommandHandler(AtlasDbContext db) : ICreateCityCommandHandler
{
    public async Task<Result<CityDto, CreateCityError>> HandleAsync(CreateCityCommand useCase, CancellationToken cancellationToken = default)
    {
        ... // only valid use cases get here; return the city, or new CityNameTaken(namesake.Id)
    }
}

builder.Services.AddUseCases([typeof(CreateCityCommand).Assembly]);
builder.Services.AddUseCaseFilter<AuditFilter>(); // optional: runs for every use case

app.MapPost("cities", async (CreateCityCommand command, ICreateCityCommandHandler handler, CancellationToken ct) =>
    (await handler.HandleAsync(command, ct)).ToHttpResult());
  • Declared errors: a handler returns Hlibz.Results' Result<TResult, TError>, TError a union of error types, each with its own data. Clients switch over them exhaustively, and documentation lists every one. (IUseCaseHandler<TUseCase, TResult> and IUseCaseHandler<TUseCase> remain for errors left undeclared.)
  • A source generator lists the handlers at compile time (no assembly scan) and wraps each one in a pipeline: validation, your IUseCaseFilters, the handler. Clients inject the handler's own interface; there is no mediator.
  • Validation without ASP.NET Core that fails exactly as ASP.NET Core's AddValidation() does: record parameter attributes, nested objects and collection items (Costs[0].Value), IValidatableObject, async rules. An invalid use case fails with Hlibz.Results' ambient ValidationFailed, its messages keyed by property path, and never reaches the handler.
  • Rules that describe themselves: IDescribedRule and [RuleSummary] say what a custom rule checks, shaped like JSON Schema, for an OpenAPI document or a tool schema to publish.
  • A handler it can't register is a build error (HUC001-HUC007).

Targets .NET 11 (C# 15 unions). .NET 10 apps stay on 1.x.

A family of packages

One of five packages I build to set up new .NET APIs the way I want them: Hlibz.Results, Hlibz.UseCases, Hlibz.MinimalApi.Endpoints, Hlibz.OpenApi and Hlibz.Paging. Each works on its own, and they work best together, as in Hlibz.Stack. New minor and major versions come often, a new major whenever .NET gives me a better way to do something. Breaking changes only ever come in a major and are listed first in its release notes. You're welcome to use the packages, and to open an issue when something is wrong or missing.

Full documentation and samples: github.com/hlib-zinchenko/Hlibz.UseCases.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
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 Hlibz.UseCases:

Package Downloads
Hlibz.OpenApi.UseCases

Publishes Hlibz.UseCases in OpenAPI documents built with Microsoft.AspNetCore.OpenApi: the rules use cases describe (IDescribedRule, [RuleSummary]) as schema keywords and text, DataAnnotations on record constructor parameters, and the 400 of the operations whose use case can fail validation.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-preview.2 0 11/27/2026
2.0.0-preview.1 0 11/27/2026
1.0.0 92 11/25/2026
1.0.0-preview.2 60 11/23/2026
1.0.0-preview.1 79 11/22/2026