API Reference Result - ulfbou/Zentient.Results GitHub Wiki

๐Ÿ“˜ API Reference: class โ€“ Result

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


๐Ÿ“– Summary

Represents the standard non-generic implementation of IResult, encapsulating success/failure state, structured errors, diagnostic messages, and a semantic status code for operations that do not return a value.

Result provides a reliable, fluent foundation for command-style or void-returning operations in a consistent, exception-free manner.


๐Ÿ’ก Design Philosophy / Rationale

The Result class follows a functional, declarative, and transport-agnostic model, ideal for outcome-based programming. It encourages the propagation of structured failures over exceptions and facilitates clear control flow.

Key Design Principles:

  • ๐Ÿ” Binary Outcome Semantics โ€“ Clear success/failure via IsSuccess, IsFailure.
  • ๐Ÿงฑ Structured Error Modeling โ€“ Integrates ErrorInfo for semantic error representation.
  • ๐Ÿšซ Void-Compatible โ€“ Designed for operations where no return value is needed.
  • โš™๏ธ Composable & Adapter-Ready โ€“ Works well in CQRS, middleware, and HTTP/gRPC layers.
  • ๐Ÿ“‘ Trace-Aware โ€“ Supports diagnostic messages for enhanced observability and auditing.

๐Ÿ”– Type Signature

public sealed class Result : IResult

๐Ÿ—๏ธ Constructors

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

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

  • Summary: Initializes a fully-defined result instance with explicit outcome metadata.

  • Parameters:

    • errors (IReadOnlyList<ErrorInfo>): List of structured errors (empty for success).
    • status (IResultStatus): Semantic result status.
    • messages (IReadOnlyList<string>): Diagnostic messages (optional).
  • Remarks: Use static factory methods for clearer semantics and intent.


๐Ÿญ Static Factory Methods

2.1. Success()

  • Signature: public static Result Success()
  • Summary: Returns a success result with no errors.
  • Return Value: Result: An instance with IsSuccess == true, no errors, and ResultStatus.Success.
var result = Result.Success();

2.2. Failure(...)

  • Signature: public static Result Failure(IEnumerable<ErrorInfo> errors, IResultStatus status)

  • Summary: Creates a failed result with errors and a failure status.

  • Parameters:

    • errors (IEnumerable<ErrorInfo>): One or more structured errors.
    • status (IResultStatus): Semantic status.
  • Return Value: Result: A failed result instance.


2.3. FromException(...)

  • Signature: public static Result FromException(Exception exception)

  • Summary: Converts an unhandled exception into a failed result.

  • Parameters:

    • exception (Exception): The thrown exception.
  • Return Value: Result: A failed result with an ErrorInfo derived from the exception.


๐Ÿ“ฆ Properties

3.1. Errors

  • Type: IReadOnlyList<ErrorInfo>
  • Summary: Structured list of errors.
  • Behavior: Empty on success. Each error carries rich metadata.

3.2. Messages

  • Type: IReadOnlyList<string>
  • Summary: Diagnostic or audit messages associated with the operation.
  • Behavior: Optional, always present (can be empty).

3.3. Status

  • Type: IResultStatus
  • Summary: Protocol-agnostic status (e.g., validation error, unauthorized).
  • Behavior: Used for mapping to HTTP/gRPC/etc.

3.4. IsSuccess

  • Type: bool
  • Summary: Indicates whether the operation succeeded.
  • Behavior: true if Errors is empty.

3.5. IsFailure

  • Type: bool
  • Summary: Indicates whether the operation failed.
  • Behavior: true if Errors.Count > 0.

3.6. ErrorMessage

  • Type: string?
  • Summary: Shortcut to the first error's message.
  • Behavior: null if the result is successful.

๐Ÿงช Usage Examples

public IResult SaveUser(User user)
{
    if (!user.IsValid())
    {
        return Result.Failure(new[] {
            ErrorInfo.Create("InvalidUser", "User failed validation.")
        }, ResultStatus.FailedValidation);
    }

    userRepository.Save(user);
    return Result.Success();
}
var result = SaveUser(user);

if (result.IsFailure)
{
    logger.LogError(result.ErrorMessage);
}

โš ๏ธ Remarks

  • Designed for operations where no return value is expected (void semantics).
  • Should be composed with extension methods such as Bind, Then, and Match.
  • Implements IResult and guarantees immutability, serialization compatibility, and structured behavior.

๐Ÿงฉ Integration and Interoperability

Component / Layer Role
CQRS Command Handlers Common return type for CommandResult-style operations
HTTP Adapters Converts into ProblemDetails via Zentient.Endpoints.Http
gRPC Translated to status metadata using GrpcResultAdapter
Telemetry/Tracing Supports diagnostics via Messages and Zentient.Telemetry hooks
Validation Composed with rule engine outputs via ErrorInfo.Create(...)

๐Ÿ“š See Also


๐Ÿท๏ธ Tags

#API #VoidResult #ErrorHandling #Immutability #ZentientCore #StructuredErrors


Last Updated: 2025-06-21 Version: 0.4.0

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