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

๐Ÿ“˜ API Reference: class โ€“ Result<T>

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


๐Ÿ“– Summary

Represents the concrete, immutable implementation of the generic result interface IResult<T>, encapsulating a typed value on success or structured errors on failure, along with diagnostic messages and a semantic result status.

Result<T> is the backbone of typed outcome modeling across Zentient applicationsโ€”powering command/query results, service outcomes, and transport-agnostic adapters.


๐Ÿ’ก Design Philosophy / Rationale

The Result<T> class adheres to a functional, declarative, and immutable model. It allows precise, type-safe control over operation results without resorting to exceptions or ambiguous null patterns.

Guiding Principles:

  • โœ… Immutability โ€“ Once created, a Result<T> cannot change state.
  • ๐Ÿงญ Semantic Clarity โ€“ Makes success/failure explicit, promoting reliable control flows.
  • ๐Ÿ”€ Structured Metadata โ€“ Encodes status, traceability, and errors as first-class citizens.
  • ๐Ÿ”ง Adapter Ready โ€“ Suitable for seamless protocol mapping (e.g., ProblemDetails, gRPC).
  • โ™ป๏ธ Extensible API Surface โ€“ Designed for use with fluent extensions (Map, Bind, Match).

๐Ÿ”– Type Signature

public sealed class Result<T> : IResult<T>

๐Ÿ—๏ธ Constructors

1.1. Result<T>(T? value, IReadOnlyList<ErrorInfo>, IResultStatus, IReadOnlyList<string>)

  • Signature: public Result(T? value, IReadOnlyList<ErrorInfo> errors, IResultStatus status, IReadOnlyList<string> messages)

  • Summary: Constructs a fully-defined result instance with explicit state.

  • Parameters:

    • value (T?): The result value (may be null for failure).
    • errors (IReadOnlyList<ErrorInfo>): The error list (empty if successful).
    • status (IResultStatus): Semantic status of the result.
    • messages (IReadOnlyList<string>): Diagnostic or user-facing messages.
  • Remarks: Used internally and by factory methods. Prefer Success(...) or Failure(...) for instantiation.


๐Ÿญ Static Factory Methods

2.1. Success(T value)

  • Signature: public static Result<T> Success(T value)

  • Summary: Creates a successful result with a provided value.

  • Parameters:

    • value (T): The success result value.
  • Return Value: Result<T>: A success result containing the given value.

  • Behavior: Initializes an empty Errors list and Status.Success.

var result = Result<string>.Success("OK");

2.2. Failure(...)

  • Signature: public static Result<T> Failure(T? value, IEnumerable<ErrorInfo>, IResultStatus)

  • Summary: Creates a failed result instance with structured error information.

  • Parameters:

    • value (T?): Optional payload to include.
    • errors (IEnumerable<ErrorInfo>): One or more error descriptors.
    • status (IResultStatus): Failure status.
  • Return Value: Result<T>: A failed result instance.

var result = Result<string>.Failure(null, new[] { ErrorInfo.Create(...)} , ResultStatus.FailedValidation);

2.3. FromException(...)

  • Signature: public static Result<T> FromException(T? value, Exception ex)

  • Summary: Constructs a failed result from an unhandled exception.

  • Parameters:

    • value (T?): Optional value to preserve.
    • ex (Exception): The thrown exception.
  • Return Value: Result<T>

  • Behavior: Automatically converts exception to ErrorInfo with diagnostic metadata.


๐Ÿ“ฆ Properties

3.1. Value

  • Type: T
  • Summary: The success value of the result.
  • Behavior: Only populated when IsSuccess == true.

3.2. Errors

  • Type: IReadOnlyList<ErrorInfo>
  • Summary: Structured error list associated with this result.
  • Behavior: Empty if the result succeeded.

3.3. Messages

  • Type: IReadOnlyList<string>
  • Summary: Supplementary trace messages.
  • Behavior: May include debug hints or observability logs.

3.4. Status

  • Type: IResultStatus
  • Summary: Protocol-neutral semantic result code.
  • Behavior: Used for mapping to gRPC, HTTP, etc.

3.5. ErrorMessage

  • Type: string?
  • Summary: Shortcut to the first errorโ€™s message, if available.

3.6. IsSuccess / IsFailure

  • Type: bool
  • Summary: Indicates the binary result state.

๐Ÿงช Usage Examples

Result<int> ParseId(string input)
{
    if (int.TryParse(input, out var id))
        return Result<int>.Success(id);

    return Result<int>.Failure(default, new[] {
        ErrorInfo.Create("InvalidId", "The input is not a valid integer.")
    }, ResultStatus.FailedValidation);
}
var result = ParseId("42");

if (result.IsSuccess)
    Console.WriteLine(result.Value);
else
    Console.WriteLine($"Failed: {result.ErrorMessage}");

โš ๏ธ Remarks

  • Result<T> is immutable; mutation after construction is unsupported.
  • Designed for use with functional pipelines via extension methods.
  • Use FromException(...) for fallback handling of unexpected failures.

๐Ÿงฉ Integration and Interoperability

  • CQRS Pipelines: Used for returning values in commands/queries.
  • HTTP Adapters: Mapped to ProblemDetails for transport translation.
  • gRPC: Exposed via GrpcResult<T>, carrying both value and trailers.
  • Serialization: Optimized for System.Text.Json and JSON-RPC contracts.

๐Ÿ“š See Also

  • IResult<T>
  • ErrorInfo
  • Result
  • ResultStatus
  • Map
  • Bind

๐Ÿท๏ธ Tags

#API #ResultModel #TypedResult #ErrorHandling #Immutability #FunctionalCore #ZentientCore


Last Updated: 2025-06-21 Version: 0.4.0

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