Skip to content

Web and Data

github-actions[bot] edited this page Nov 30, 2026 · 2 revisions

Web & Data Integrations (Ferrous.AspNetCore & Ferrous.EntityFrameworkCore)

Both packages are opt-in so console apps and class libraries installing Ferrous do not pull ASP.NET Core or EF Core transitively.


ASP.NET Core Minimal APIs (Ferrous.AspNetCore)

dotnet add package Ferrous.AspNetCore

Explicit Result / Option Mapping

Map Result<T, TError> and Option<T> directly to IResult (200 OK, 404 Not Found, or RFC 7807 ProblemDetails). When TError is a ContextError, the full causal chain is preserved in detail and in the errorChain extension field:

using Ferrous.AspNetCore;
using Ferrous.Results;

app.MapGet("/albums/{id}", async (AlbumId id, AlbumService svc) =>
    (await svc.FindAlbumAsync(id))
        .ToHttpResult(notFoundDetail: $"Album {id.Value} was not found."));

app.MapPost("/orders", async (CreateOrderRequest req, OrderService svc) =>
    (await svc.PlaceOrderAsync(req))
        .WithContext("checkout failed")
        .ToHttpResult(StatusCodes.Status422UnprocessableEntity));

Automatic Endpoint Filter Unwrapping

Attach .WithFerrousResults() to an endpoint or route group so handlers can return Result<T, TError> or Option<T> directly (zero reflection, NativeAOT-compatible):

var api = app.MapGroup("/api").WithFerrousResults();

api.MapGet("/tracks/{id}", (TrackId id) => repo.FindTrack(id)); // returns Option<Track>
api.MapPost("/tracks", (CreateTrack cmd) => repo.Create(cmd));  // returns Result<Track, ContextError>

OpenAPI Schema Transformation (.NET 10+)

Register .AddFerrousOpenApi() so [Newtype], NonEmptyString, NonZero<T>, and [DiscriminatedUnion] emit clean primitive OpenAPI schemas (int32, uuid, string with minLength: 1) instead of wrapper objects:

builder.Services.AddOpenApi(options => options.AddFerrousOpenApi());

Entity Framework Core (Ferrous.EntityFrameworkCore)

dotnet add package Ferrous.EntityFrameworkCore

Value Converters for Refined Types & [Newtype]

Register built-in converters for NonEmptyString and NonZero<T> in ConfigureConventions, and wire reflection-free [Newtype] converters per property:

using Ferrous.EntityFrameworkCore;

public sealed class MusicDbContext(DbContextOptions<MusicDbContext> options) : DbContext(options)
{
    public DbSet<Album> Albums => Set<Album>();

    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
        => configurationBuilder
            .UseFerrousConventions()
            .UseFerrousNewtypes<MusicDbContext>(); // auto-discovers all [Newtype] structs in the assembly!

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Album>(e =>
        {
            e.HasKey(a => a.Id);
            e.Property(a => a.Id).HasNewtypeConversion(id => id.Value, raw => new AlbumId(raw));
        });
    }
}

Result- & Option-Returning Query Helpers

Replace FirstOrDefaultAsync (null / default(T) ambiguity) and SingleAsync (throws InvalidOperationException) with explicit, value-type-safe helpers:

Option<Album> maybeAlbum = await db.Albums.FirstOrOptionAsync(a => a.Id == id, ct);

Result<Album, string> album = await db.Albums.SingleOrErrAsync(
    a => a.Title == title,
    notFoundError: () => "Album not found",
    multipleFoundError: () => "Multiple albums matched",
    ct);

Clone this wiki locally