API Reference IResult T - ulfbou/Zentient.Results GitHub Wiki

๐Ÿ“˜ API Reference: interface โ€“ IResult<T>

Namespace: Zentient.Results Assembly: Zentient.Results.dll Available since: v0.1.0


๐Ÿ“– Summary

Represents a generic result type that combines binary success/failure semantics with an optional typed value for successful outcomes, alongside structured errors, messages, and semantic status.

IResult<T> is the standard abstraction for returning value-rich results while preserving error-awareness, observability, and transport-agnostic semantics.


๐Ÿ’ก Design Philosophy / Rationale

IResult<T> builds upon the IResult interface by introducing a typed success value (T). It enables pure functional result composition while allowing full propagation of structured error information and status metadata.

Key principles include:

  • ๐Ÿงฐ Composability with Typed Values โ€” Enables fluent Map, Bind, and monadic operations.
  • ๐Ÿ“ฆ Structured Failure Modeling โ€” Inherits all capabilities of IResult.
  • ๐Ÿงช Observability-Ready โ€” Includes Messages for diagnostics and telemetry pipelines.
  • ๐Ÿ”— Interoperability โ€” Compatible with transport adapters and protocol-mapped status models.
  • ๐Ÿ”’ Immutability by Contract โ€” Result objects are intended to be read-only after creation.

๐Ÿ”– Type Signature

public interface IResult<T> : IResult

๐Ÿ“ฆ Properties

1. Value

  • Type: T
  • Summary: The value returned by the operation if it succeeded.
  • Behavior: Only populated when IsSuccess is true. May throw if accessed when IsFailure is true.
  • Remarks: Safe extraction should use .Unwrap() or .GetValueOrDefault(...) extension methods.

Inherited Properties from IResult

Refer to IResult for:

  • IsSuccess (bool)
  • IsFailure (bool)
  • Errors (IReadOnlyList<ErrorInfo>)
  • Messages (IReadOnlyList<string>)
  • ErrorMessage (string?)
  • Status (IResultStatus)

๐Ÿ”ง Methods (Extension-Based Functional API)

Extension methods available for IResult<T> (see ResultTransformationExtensions):

Method Description
Map<TIn, TOut>(...) Transforms the value of a successful result to a new type.
Bind<TIn, TOut>(...) Applies a result-returning function to a successful result.
Then<TIn>(...) Executes an action if the result is successful; otherwise propagates failure.
GetValueOrDefault(...) Returns the result's value or a fallback default.
Unwrap() Extracts the value or throws if the result is a failure.

๐Ÿงช Usage Examples

IResult<User> result = userService.GetUserById(id);

if (result.IsSuccess)
{
    var user = result.Value;
    Console.WriteLine($"Welcome, {user.FullName}!");
}
else
{
    logger.LogError("User fetch failed: {0}", result.ErrorMessage);
}

Composition example:

var userDisplayResult = userService.GetUserById(id)
    .Map(user => user.FullName)
    .Bind(name => emailService.SendWelcomeEmail(name));

๐Ÿงญ Semantic Layers

Layer Member(s) Purpose
Outcome Flag IsSuccess, IsFailure Primary success/failure indicator.
Value Value, Map, Bind Access and transformation of typed results.
Error Context Errors, ErrorMessage Structured diagnostics and failure analysis.
Semantic Status Status Status-driven protocol interoperability (e.g., HTTP, gRPC).
Trace Messages Messages Hints, logs, and audit trail data.

๐Ÿ” Implementations

Type Description
Result<T> Default generic result implementation.
HttpResult<T> HTTP-specific typed result using ProblemDetails.
GrpcResult<T> Typed result with gRPC code and trailer metadata.

โš ๏ธ Remarks

  • Always check IsSuccess before accessing Value.
  • Use .Unwrap() with caution: it throws on failure.
  • Supports fluent error-aware pipelines in CQRS, service layers, and adapters.
  • Suitable for async-first design using Task<IResult<T>> or ValueTask<IResult<T>>.

๐Ÿงฉ Integration and Interoperability

Layer Role
CQRS Handlers Used as a return type for queries and commands with typed outputs.
Adapters Adapted via HttpResultAdapter<T>, GrpcResultAdapter<T>.
Observability Messages enriched by Zentient.Telemetry.ITelemetryEnricher.
Functional APIs Consumed by Map, Bind, Then, and other transformation methods.

๐Ÿ“š See Also

  • IResult
  • Result<T>
  • ErrorInfo
  • ResultTransformationExtensions
  • IResultStatus

๐Ÿท๏ธ Tags

#API #GenericResult #FunctionalCore #Immutable #FluentAPI #ErrorHandling #TypedResults #ZentientCore


Last Updated: 2025-06-21 Version: 0.4.0

โš ๏ธ **GitHub.com Fallback** โš ๏ธ