Skip to content

Search EricksonLopez.* Ecosystem

Type a library name, NuGet package ID, .NET type, or architectural concept to search across the portal.

Result Pattern & Explicit Errors

EricksonLopez.Result (ADR-001) models expected domain and application failures as explicit readonly struct return values rather than thrown exceptions. It sits at L0 as the central dependency hub for 21 sibling library families (20 published + 1 unpublished local observability).

  • Result: Non-generic readonly partial struct for commands or operations that succeed with no return value (Result.Success()) or fail with an Error (Result.Failure(error)).
  • Result<TValue>: Generic readonly struct carrying either a TValue or an Error.
  • Result<TValue, TError> (EricksonLopez.Result.Generic): Typed custom error variant when callers require a domain-specific error enum or union instead of Error.
  • Maybe<T> (EricksonLopez.Result.Maybe): Optional presence wrapper that converts cleanly to Result<T> via .ToResult(Error).

The TState overloads on Ensure, Map, Bind, and Tap prevent closure allocations (RESULT004):

using System;
using EricksonLopez.Result;
namespace Example.ResultGuide;
public static class AccountWorkflow
{
public static Result<decimal> ComputeNetPayout(decimal grossAmount, decimal feeRate)
{
return Result.Success(grossAmount)
.Ensure(
static amount => amount > 0m,
Error.Validation("Payout.NonPositive", "Gross amount must be positive."))
.Ensure(
feeRate,
static (rate, _) => rate is >= 0m and < 1m,
Error.Validation("Payout.InvalidFeeRate", "Fee rate must be in [0, 1)."))
.Map(
feeRate,
static (rate, amount) => amount * (1m - rate));
}
public static string FormatOutcome(in Result<decimal> outcome) =>
outcome.Match(
onSuccess: static net => $"Net payout: {net:F2}",
onFailure: static err => $"Rejected [{err.Code}]: {err.Description}");
}

When validating user input where all field errors should be surfaced together (rather than failing on the first check), use Result.ValidateAll:

using EricksonLopez.Result;
namespace Example.ResultGuide;
public static class RegistrationRules
{
public static Result ValidateInput(string email, int age) =>
Result.ValidateAll(
() => string.IsNullOrWhiteSpace(email)
? Result.Failure(Error.Validation("User.EmailRequired", "Email is required."))
: Result.Success(),
() => age < 18
? Result.Failure(Error.Validation("User.Underage", "Age must be at least 18."))
: Result.Success());
}