Alvecta.Foundation.Application.Contracts 11.2.108

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

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). A Result is a struct, so IUseCase<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.
  • TError is held to notnull only, so that an error may be a union of the families of errors a use case can return (union Error(SourceError, ContentError)). The Alvecta.Utils README lists the limits of a union as an error.
  • A use case without a response succeeds with Done when 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, an IUseCaseWithoutRequestAndResponse.

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. Alvecta.Foundation.Infrastructure.WebApi is its counterpart in an application that calls such a host: it registers a use-case interface as a call over HTTP, which hands back the response or the error the use case returned on the host.

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

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
11.2.108 41 12/3/2026
11.1.107 46 12/3/2026
11.0.106 47 12/3/2026
10.4.105 68 12/3/2026
10.4.104 58 12/2/2026
10.4.103 54 12/2/2026
10.4.102 77 12/1/2026
10.3.101 111 11/30/2026
10.2.100 102 11/30/2026
10.2.99 101 11/30/2026
10.1.98 100 11/30/2026
10.0.97 111 11/29/2026
9.6.96 109 11/25/2026
9.6.95 105 11/25/2026
9.6.94 110 11/24/2026
9.6.93 102 11/24/2026
9.5.92 103 11/24/2026
9.4.91 109 11/20/2026
9.3.90 109 11/13/2026
9.2.89 99 11/12/2026
Loading failed