API Reference ResultCreationHelpersExtensions - ulfbou/Zentient.Results GitHub Wiki
namespace Zentient.ResultsProvides 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.
| Property | Value |
|---|---|
| 📦 Assembly | Zentient.Results.dll |
| 👁️ Visibility | public static |
| ⚙️ Type Kind | static class |
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 |
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.
public static IResult AsSuccess(this string message)Wraps a string as a success message in a non-generic IResult.
Throws
ArgumentExceptionifmessageis null or empty.
public static IResult<T> AsSuccess<T>(this T value, string? message = null)Wraps a value into a successful IResult<T> with optional message.
public static IResult AsError(this string message, string code = "GeneralError")Creates a failure IResult from an error message and optional error code.
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.
public static IResult AsError(this ErrorInfo error)Creates a failure IResult from a single ErrorInfo.
public static IResult<T> AsError<T>(this ErrorInfo error, T? value)Creates a failure IResult<T> from ErrorInfo and a value.
public static IResult AsError(this IEnumerable<ErrorInfo> errors)Creates a failure IResult from multiple ErrorInfo objects.
public static IResult<T> AsError<T>(this IEnumerable<ErrorInfo> errors, T? value)Creates a failure IResult<T> from multiple errors and a value.
public static IResult AsError<TException>(this TException ex, IResultStatus? status = null) where TException : ExceptionCreates a failed non-generic IResult from an exception with optional status.
public static IResult<T> AsError<T>(this Exception ex, IResultStatus? status = null)Creates a failed generic IResult<T> from an exception with optional status.
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.
public static IResult<T> AsNoContent<T>(this T _)Returns a generic IResult<T> with NoContent status. Useful to preserve type context with no data.
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.
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>();