Repository navigation
Fluent.Unions 1.0.0
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
switchover 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, everyTaskoverload and the value-lessResult. 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,Outon the value;Catch,Do,OnErrorDo,Finallyon the error;Match,TryGetValue,GetValueOrDefaultto leave the chain. Query syntax works too. Option<T>instead ofnull.TryParse,FirstOrNone,GetValueOrNone,Or, and conversions to and from results.- Async everywhere.
Taskoverloads,...Awaitoperators,Result.WhenAll,RetryAsync,Timeoutand async streams. Cancellation is never turned into an error. - Validation that collects every error.
Validate,CombineAlland a catalogue of ready-made rules such asEnsureNotEmptyandEnsureInRange, reported asValidationErrorswith property names. - More unions.
Loadable<T>for UI state, plusIs/Asand 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 implementIResult<TSelf, T, TError>are removed. Declare the type with[FluentResult<TError>]instead; see Migrating from 0.3.0.SelectManyAwaitis removed; useChainAwait.BindAppendandBindAppendAwaitare renamed toChainAppendandChainAppendAwait.- The
Companionoption 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 exampleResultOperatorsandResultTaskOperators, so they no longer collide with your ownResultExtensions. IsSuccessandTryGetErrormoved toIErrorResult<TSelf, TError>, and a non-genericIErrorResultreads any result without reflection.ErrorResultExtensionsis removed.- Registration is named consistently:
WithFluentUnions()for MessagePack,AddFluentUnions()for Orleans,FluentUnionsDapper.Register()for Dapper. Every registration is safe to call twice. - gRPC:
ResultGrpcOptionsis configured withservices.Configure<ResultGrpcOptions>(...). AnRpcExceptioncaptured from a downstream call keeps only its status code; the original is inStatus.DebugException. - ASP.NET Core: a faulted
Task<Result<T>>propagates to the framework's exception handling instead of becoming a 500.ResultHttpOptions.GetStatusCodeis removed. default(Option<T>)now equalsOption.None, anddefault(Loadable<T>)equalsNotLoaded.
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
OnErrorDoandDofor declared types, and the FLU0012 code fix only appears when the result compiles. RetryAsyncretries 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
DateTimecolumns toDateTimeOffset. Verify keeps the labels of nested unions. HotChocolate unwrapsResult<Option<T>>to a nullable field. - Result type detection and regular expressions are cached.
Full Changelog: v0.3.0...v1.0.0