Alvecta.Foundation.Application.Contracts
10.3.101
See the version list below for details.
dotnet add package Alvecta.Foundation.Application.Contracts --version 10.3.101
NuGet\Install-Package Alvecta.Foundation.Application.Contracts -Version 10.3.101
<PackageReference Include="Alvecta.Foundation.Application.Contracts" Version="10.3.101" />
<PackageVersion Include="Alvecta.Foundation.Application.Contracts" Version="10.3.101" />
<PackageReference Include="Alvecta.Foundation.Application.Contracts" />
paket add Alvecta.Foundation.Application.Contracts --version 10.3.101
#r "nuget: Alvecta.Foundation.Application.Contracts, 10.3.101"
#:package Alvecta.Foundation.Application.Contracts@10.3.101
#addin nuget:?package=Alvecta.Foundation.Application.Contracts&version=10.3.101
#tool nuget:?package=Alvecta.Foundation.Application.Contracts&version=10.3.101
Alvecta.Foundation.Application.Contracts
The use-case contracts of the Alvecta application layer: the interfaces a presentation adapter (an HTTP endpoint, a CLI command, a message consumer) calls, and that the application layer implements.
It belongs to the Application.Contracts layer. Contracts may speak the domain vocabulary
(value objects) but never the domain model, so a presentation project can reference them without
seeing entities or use-case implementations. Its one dependency is Alvecta.Utils, for
Result<T, TError> and Done.
What it contains
Namespace Alvecta.Foundation.Application.Contracts.Base holds the use-case interfaces. Every
shape of a use case has an interface name of its own, in a plain form and in a form with an error:
| Shape | Plain | With an error |
|---|---|---|
| a request and a response | IUseCase<TRequest, TResponse> |
IUseCase<TRequest, TResponse, TError> |
| a response and no request | IUseCaseWithoutRequest<TResponse> |
IUseCaseWithoutRequest<TResponse, TError> |
| a request and no response | IUseCaseWithoutResponse<TRequest> |
IUseCaseWithoutResponse<TRequest, TError> |
| neither | IUseCaseWithoutRequestAndResponse |
IUseCaseWithoutRequestAndResponse<TError> |
The name IUseCase is only for a use case that has both a request and a response. A shape that
lacks one or both says so in its name.
Each interface declares one method, ExecuteAsync, which takes the request, when the shape has
one, and a CancellationToken. What it returns:
| Plain | With an error | |
|---|---|---|
| a shape with a response | Task<TResponse> |
Task<Result<TResponse, TError>> |
| a shape without a response | Task |
Task<Result<Done, TError>> |
The words have one meaning each, so that "response" is never also "whatever comes back":
| Word | Meaning |
|---|---|
| request | what the caller hands a use case |
| response | what the use case hands back when it did what was asked; never the error |
| error | why it did not, as facts |
| result | the one or the other: Result<TResponse, TError> |
| without response | it succeeds with nothing to hand back; it may still return an error |
- A failure the caller is expected to handle is a value. A use case that can meet one takes the form with an error and returns a result. An exception stays for the unexpected.
- Every interface declares its own
ExecuteAsync. A form with an error does not derive from a plain one, so a use case with an error is never also a plain use case whose response is a result. - Requests and responses are classes (
where TRequest : class,where TResponse : class). AResultis a struct, soIUseCase<Request, Result<Response, Error>>does not compile (CS0452). There is exactly one way to declare a use case that may return an error, and a result can never be taken for a plain response and serialized. TErroris held tonotnullonly, so that an error may be a union of the families of errors a use case can return (union Error(SourceError, ContentError)). TheAlvecta.UtilsREADME lists the limits of a union as an error.- A use case without a response succeeds with
Donewhen it may return an error:Result<Done, TError>, so that one result type serves every shape.
The namespace also holds IRequestValidator<TRequest>: void Validate(TRequest), every
request-level rule of one use case, run by a validating decorator in front of it.
Namespace Alvecta.Foundation.Application.Contracts.Database:
IMigrateDatabaseUseCase— the standard "apply the database migrations" use case, anIUseCaseWithoutRequestAndResponse.
Version 10.0 renamed two interfaces, so that the name IUseCase says one shape:
| Until 9.x | From 10.0 |
|---|---|
IUseCase<TRequest> |
IUseCaseWithoutResponse<TRequest> |
IUseCase |
IUseCaseWithoutRequestAndResponse |
Usage
dotnet add package Alvecta.Foundation.Application.Contracts
Declare the use case in your *.Application.Contracts project. Nesting the request, the response
and the error inside the use-case interface keeps each use case in one file:
using Alvecta.Foundation.Application.Contracts.Base;
public interface IGetOrderUseCase : IUseCase<IGetOrderUseCase.Request, IGetOrderUseCase.Response>
{
public sealed record Request(OrderId OrderId);
public sealed record Response(OrderId OrderId, OrderStatus Status);
}
Implement it in your *.Application project and register it with DI:
public sealed class GetOrderUseCase(IOrdersQueryService orders) : IGetOrderUseCase
{
public async Task<IGetOrderUseCase.Response> ExecuteAsync(
IGetOrderUseCase.Request request,
CancellationToken cancellationToken)
{
var status = await orders.GetStatusAsync(request.OrderId, cancellationToken);
return new(request.OrderId, status);
}
}
services.AddScoped<IGetOrderUseCase, GetOrderUseCase>();
A use case that can meet a failure its caller is expected to handle names the error as its third type argument. The error carries facts and no words:
public interface IPlaceOrderUseCase
: IUseCase<IPlaceOrderUseCase.Request, IPlaceOrderUseCase.Response, IPlaceOrderUseCase.Error>
{
public sealed record Request(ProductId ProductId, Quantity Quantity);
public sealed record Response(OrderId OrderId);
public closed record Error
{
public sealed record OutOfStock(ProductId ProductId) : Error;
public sealed record Discontinued(ProductId ProductId) : Error;
}
}
Its implementation returns the response or the error as it is; either converts to the result:
using Alvecta.Utils;
public sealed class PlaceOrderUseCase(IStockQueryService stock, IOrdersService orders) : IPlaceOrderUseCase
{
public async Task<Result<IPlaceOrderUseCase.Response, IPlaceOrderUseCase.Error>> ExecuteAsync(
IPlaceOrderUseCase.Request request,
CancellationToken cancellationToken)
{
if (!await stock.IsAvailableAsync(request.ProductId, request.Quantity, cancellationToken))
{
return new IPlaceOrderUseCase.Error.OutOfStock(request.ProductId);
}
var orderId = await orders.PlaceAsync(request.ProductId, request.Quantity, cancellationToken);
return new IPlaceOrderUseCase.Response(orderId);
}
}
Alvecta.Foundation.Presentation.WebApi.AspNetCore maps these interfaces to minimal-API endpoints,
with a method named after each interface.
| Product | Versions 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. |
-
net10.0
- Alvecta.Utils (>= 9.1.92)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Alvecta.Foundation.Application.Contracts:
| Package | Downloads |
|---|---|
|
Alvecta.Foundation.Api.AspNetCore
Package Description |
|
|
Alvecta.Foundation.Presentation.WebApi.AspNetCore
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.4.102 | 0 | 12/1/2026 |
| 10.3.101 | 63 | 11/30/2026 |
| 10.2.100 | 61 | 11/30/2026 |
| 10.2.99 | 60 | 11/30/2026 |
| 10.1.98 | 60 | 11/30/2026 |
| 10.0.97 | 71 | 11/29/2026 |
| 9.6.96 | 77 | 11/25/2026 |
| 9.6.95 | 74 | 11/25/2026 |
| 9.6.94 | 98 | 11/24/2026 |
| 9.6.93 | 92 | 11/24/2026 |
| 9.5.92 | 91 | 11/24/2026 |
| 9.4.91 | 109 | 11/20/2026 |
| 9.3.90 | 109 | 11/13/2026 |
| 9.2.89 | 98 | 11/12/2026 |
| 9.1.88 | 104 | 11/11/2026 |
| 9.0.87 | 122 | 11/4/2026 |
| 9.0.86 | 114 | 11/4/2026 |
| 9.0.85 | 110 | 11/4/2026 |
| 9.0.84 | 108 | 11/4/2026 |
| 9.0.83 | 126 | 10/29/2026 |