Hlibz.UseCases
2.0.0-preview.2
dotnet add package Hlibz.UseCases --version 2.0.0-preview.2
NuGet\Install-Package Hlibz.UseCases -Version 2.0.0-preview.2
<PackageReference Include="Hlibz.UseCases" Version="2.0.0-preview.2" />
<PackageVersion Include="Hlibz.UseCases" Version="2.0.0-preview.2" />
<PackageReference Include="Hlibz.UseCases" />
paket add Hlibz.UseCases --version 2.0.0-preview.2
#r "nuget: Hlibz.UseCases, 2.0.0-preview.2"
#:package Hlibz.UseCases@2.0.0-preview.2
#addin nuget:?package=Hlibz.UseCases&version=2.0.0-preview.2&prerelease
#tool nuget:?package=Hlibz.UseCases&version=2.0.0-preview.2&prerelease
![]()
Hlibz.UseCases
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>,TErroraunionof error types, each with its own data. Clients switch over them exhaustively, and documentation lists every one. (IUseCaseHandler<TUseCase, TResult>andIUseCaseHandler<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' ambientValidationFailed, its messages keyed by property path, and never reaches the handler. - Rules that describe themselves:
IDescribedRuleand[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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Hlibz.Results (>= 3.0.0-preview.2)
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 |