Guides & Production Recipes
Practical production patterns using EricksonLopez.Result (3.0.0) with zero closure allocations and compile-time error generation.
Recipe 1: Closure-Free TState Pipelines & in Overloads
Section titled “Recipe 1: Closure-Free TState Pipelines & in Overloads”Pass local state explicitly as a TState tuple so lambdas stay static and avoid display-class allocations (RESULT004):
using EricksonLopez.Result;
namespace Example.ResultRecipes;
public static class DiscountPipeline{ public static Result<decimal> ApplyDiscount(in Result<decimal> subtotalResult, decimal discountRate, decimal minOrder) { var state = (discountRate, minOrder);
return subtotalResult .Ensure( state, static (s, amount) => amount >= s.minOrder, Error.Domain("Discount.MinimumNotMet", "Order subtotal is below discount threshold.")) .Map( state, static (s, amount) => amount * (1m - s.discountRate)); }}Recipe 2: Parallel Compound Validation (ValidateAllParallelAsync)
Section titled “Recipe 2: Parallel Compound Validation (ValidateAllParallelAsync)”Added in v3.0.0, Result.ValidateAllParallelAsync evaluates independent asynchronous validation checks concurrently and aggregates all failures into a combined Error (WellKnownErrors.CombinedFailuresCode).
Recipe 3: Compile-Time Domain Errors (DomainErrors.Generators)
Section titled “Recipe 3: Compile-Time Domain Errors (DomainErrors.Generators)”Add EricksonLopez.Result.DomainErrors.Generators and include *.errors.json / *.domainerrors.json as <AdditionalFiles> in your .csproj to emit strongly-typed static Error factories at build time (RESULT_DOM_001–003).
Recipe 4: Maybe<T> to Result<T> at Repository Boundaries
Section titled “Recipe 4: Maybe<T> to Result<T> at Repository Boundaries”using System;using EricksonLopez.Result;using EricksonLopez.Result.Maybe;
namespace Example.ResultRecipes;
public static class CustomerLookup{ public static Result<string> ResolveDisplayName(string? candidate, Guid customerId) => Maybe<string>.From(candidate) .ToResult(Error.NotFound("Customer.NotFound", $"Customer '{customerId}' was not found."));}