Alvecta.Foundation.Application.Contracts 10.0.97

There is a newer version of this package available.
See the version list below for details.
dotnet add package Alvecta.Foundation.Application.Contracts --version 10.0.97
                    
NuGet\Install-Package Alvecta.Foundation.Application.Contracts -Version 10.0.97
                    
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="10.0.97" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Alvecta.Foundation.Application.Contracts" Version="10.0.97" />
                    
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 10.0.97
                    
#r "nuget: Alvecta.Foundation.Application.Contracts, 10.0.97"
                    
#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@10.0.97
                    
#: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=10.0.97
                    
Install as a Cake Addin
#tool nuget:?package=Alvecta.Foundation.Application.Contracts&version=10.0.97
                    
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.

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
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
Loading failed