Skip to content

Fluent.Unions 1.0.0

Choose a tag to compare

@github-actions github-actions released this 21 Nov 17:41
· 32 commits to main since this release

Fluent.Unions 1.0.0 is the first production-ready release. The public API is now stable and follows semantic versioning from here on.

Result, Result<T> and Option<T> for .NET, built on the C# 15 union keyword. A method returns either its value or an error, and the compiler checks that callers handle both. Fluent operators are named after LINQ and Rx.NET, so a whole flow reads as one expression.

dotnet add package Fluent.Unions

Requires the .NET 11 SDK. Documentation: https://tobistr.github.io/Fluent.Unions/

Features

  • Compiler-checked handling. A switch over a result or option warns when a case is missing. Analyzers report ignored results, async lambdas that escape a result, unions that cannot tell their cases apart, serialized results and more, with code fixes.
  • Your own Result in one line. [FluentResult<DomainException>] public readonly partial struct Result<T>; lets the source generator build a complete result around your error type: every operator, every Task overload and the value-less Result. Make the error a union and the compiler checks that every case is handled.
  • Built for hot paths. Results store their value without boxing, and a chain of operators with static lambdas allocates nothing.
  • LINQ and Rx operators. Select, Chain, Ensure, Zip, ChainAppend, Out on the value; Catch, Do, OnErrorDo, Finally on the error; Match, TryGetValue, GetValueOrDefault to leave the chain. Query syntax works too.
  • Option<T> instead of null. TryParse, FirstOrNone, GetValueOrNone, Or, and conversions to and from results.
  • Async everywhere. Task overloads, ...Await operators, Result.WhenAll, RetryAsync, Timeout and async streams. Cancellation is never turned into an error.
  • Validation that collects every error. Validate, CombineAll and a catalogue of ready-made rules such as EnsureNotEmpty and EnsureInRange, reported as ValidationErrors with property names.
  • More unions. Loadable<T> for UI state, plus Is/As and a JSON converter for your own unions.
  • Safe by default. Results refuse to serialize in every supported serializer, so exception details cannot leak. ToString() of an error shows the type and message, never the stack trace.

Integrations

27 packages, one per library you already use:

Area Packages
Web Fluent.Unions.AspNetCore (ProblemDetails, ValidationProblemDetails, endpoint filter), Fluent.Unions.OpenApi, Fluent.Unions.Swashbuckle, Fluent.Unions.NSwag, Fluent.Unions.HotChocolate, Fluent.Unions.Grpc, Fluent.Unions.Refit
Application Fluent.Unions.FluentValidation, Fluent.Unions.Polly, Fluent.Unions.Mediator, Fluent.Unions.MediatR, Fluent.Unions.Serilog
Data and serialization Fluent.Unions.NewtonsoftJson, Fluent.Unions.MessagePack, Fluent.Unions.Orleans, Fluent.Unions.MongoDB, Fluent.Unions.Dapper, Fluent.Unions.Mapster, Fluent.Unions.AutoMapper
Testing Fluent.Unions.AwesomeAssertions, Fluent.Unions.FluentAssertions, Fluent.Unions.Shouldly, Fluent.Unions.TUnit, Fluent.Unions.NUnit, Fluent.Unions.Xunit, Fluent.Unions.Verify

System.Text.Json support and OpenTelemetry (Activity.RecordResult) are part of the core package. Every integration works with your own declared result types as well as the built-in ones.

What changed since 0.3.0

1.0.0 consolidates the API after a full review for usability, security, performance and robustness.

Breaking changes

  • Result<T, TError> and hand-written result types that implement IResult<TSelf, T, TError> are removed. Declare the type with [FluentResult<TError>] instead; see Migrating from 0.3.0.
  • SelectManyAwait is removed; use ChainAwait. BindAppend and BindAppendAwait are renamed to ChainAppend and ChainAppendAwait.
  • The Companion option of [FluentResult] is removed. The value-less companion is generated unless you declare your own [FluentResult] struct with the same name.
  • Generated operator classes are named {Type}{Group}Operators, for example ResultOperators and ResultTaskOperators, so they no longer collide with your own ResultExtensions.
  • IsSuccess and TryGetError moved to IErrorResult<TSelf, TError>, and a non-generic IErrorResult reads any result without reflection. ErrorResultExtensions is removed.
  • Registration is named consistently: WithFluentUnions() for MessagePack, AddFluentUnions() for Orleans, FluentUnionsDapper.Register() for Dapper. Every registration is safe to call twice.
  • gRPC: ResultGrpcOptions is configured with services.Configure<ResultGrpcOptions>(...). An RpcException captured from a downstream call keeps only its status code; the original is in Status.DebugException.
  • ASP.NET Core: a faulted Task<Result<T>> propagates to the framework's exception handling instead of becoming a 500. ResultHttpOptions.GetStatusCode is removed.
  • default(Option<T>) now equals Option.None, and default(Loadable<T>) equals NotLoaded.

Security

  • UnionJsonConverter (System.Text.Json and Newtonsoft.Json) no longer serializes results or options when it is registered globally.
  • ToString() of an error result no longer includes the stack trace.
  • HotChocolate raises non-exception errors as field errors instead of exposing them as values.
  • The documented domain error setup no longer sends internal exception messages to HTTP clients.

Robustness and performance

  • The source generator reports clear diagnostics (FLU0101 to FLU0103, with help links) instead of compiler errors in generated code, handles keyword identifiers and costs almost nothing in projects without [FluentResult].
  • Analyzer fixes: no false FLU0001 on OnErrorDo and Do for declared types, and the FLU0012 code fix only appears when the result compiles.
  • RetryAsync retries timeouts that are not caller cancellation. Async-stream helpers, assertion packages, NUnit, Serilog and Verify decide Ok and Error by the result's state.
  • ASP.NET Core logs 5xx responses, treats empty results consistently and maps wrapped validation errors to ValidationProblemDetails.
  • Dapper maps enums stored as text and DateTime columns to DateTimeOffset. Verify keeps the labels of nested unions. HotChocolate unwraps Result<Option<T>> to a nullable field.
  • Result type detection and regular expressions are cached.

Full Changelog: v0.3.0...v1.0.0