API Reference ResultCreationHelpersExtensions - ulfbou/Zentient.Results GitHub Wiki

📘 API Reference: static classZentient.Results.ResultCreationHelpersExtensions

namespace Zentient.Results

📖 Summary

Provides fluent extension methods to simplify creation of IResult and IResult<T> instances from common .NET types such as bool, string, Exception, and ErrorInfo collections. Enables expressive, readable result construction for functional-style workflows.


📌 Metadata

Property Value
📦 Assembly Zentient.Results.dll
👁️ Visibility public static
⚙️ Type Kind static class

🔧 Extension Methods

AsResult(this bool isSuccess, ...)

public static IResult AsResult(this bool isSuccess, string? successMessage = null, ErrorInfo? failureError = null)

Creates a non-generic IResult based on a boolean flag.

Parameter Type Description
isSuccess bool Indicates success (true) or failure (false).
successMessage string? Optional message to attach on success.
failureError ErrorInfo? Optional error to associate on failure.

| Returns | IResult instance |


AsResult<T>(this bool isSuccess, T value, ...)

public static IResult<T> AsResult<T>(this bool isSuccess, T value, string? successMessage = null, ErrorInfo? failureError = null)

Creates a generic IResult<T> from a boolean flag and a value, with optional success message or failure error.


🟢 AsSuccess(this string message)

public static IResult AsSuccess(this string message)

Wraps a string as a success message in a non-generic IResult.

Throws ArgumentException if message is null or empty.


🟢 AsSuccess<T>(this T value, string? message = null)

public static IResult<T> AsSuccess<T>(this T value, string? message = null)

Wraps a value into a successful IResult<T> with optional message.


AsError(this string message, string code = "GeneralError")

public static IResult AsError(this string message, string code = "GeneralError")

Creates a failure IResult from an error message and optional error code.


AsError<T>(this string message, T? value, string code = "GeneralError")

public static IResult<T> AsError<T>(this string message, T? value, string code = "GeneralError")

Creates a failure IResult<T> from an error message, value, and error code.


AsError(this ErrorInfo error)

public static IResult AsError(this ErrorInfo error)

Creates a failure IResult from a single ErrorInfo.


AsError<T>(this ErrorInfo error, T? value)

public static IResult<T> AsError<T>(this ErrorInfo error, T? value)

Creates a failure IResult<T> from ErrorInfo and a value.


AsError(this IEnumerable<ErrorInfo> errors)

public static IResult AsError(this IEnumerable<ErrorInfo> errors)

Creates a failure IResult from multiple ErrorInfo objects.


AsError<T>(this IEnumerable<ErrorInfo> errors, T? value)

public static IResult<T> AsError<T>(this IEnumerable<ErrorInfo> errors, T? value)

Creates a failure IResult<T> from multiple errors and a value.


💥 AsError<TException>(this TException ex, IResultStatus? status = null)

public static IResult AsError<TException>(this TException ex, IResultStatus? status = null) where TException : Exception

Creates a failed non-generic IResult from an exception with optional status.


💥 AsError<T>(this Exception ex, IResultStatus? status = null)

public static IResult<T> AsError<T>(this Exception ex, IResultStatus? status = null)

Creates a failed generic IResult<T> from an exception with optional status.


💥 AsError<T>(this Exception ex, T? value, IResultStatus? status = null)

public static IResult<T> AsError<T>(this Exception ex, T? value, IResultStatus? status = null)

Creates a failed IResult<T> from an exception and value with optional status.


🚫 AsNoContent<T>(this T _)

public static IResult<T> AsNoContent<T>(this T _)

Returns a generic IResult<T> with NoContent status. Useful to preserve type context with no data.


🧠 Remarks

These helpers provide a concise and expressive API for creating IResult instances from common .NET types, unifying success/failure semantics across layers such as domain, transport, and infrastructure.


✅ Example Usage

bool successFlag = true;
IResult successResult = successFlag.AsResult("Operation completed successfully");

string errorMessage = "Failed to load configuration";
IResult failureResult = errorMessage.AsError();

Exception ex = new InvalidOperationException("Unexpected error");
IResult<ConfigDto> errorWithException = ex.AsError<ConfigDto>();
⚠️ **GitHub.com Fallback** ⚠️