Struct Result
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Non-generic Result utility host containing factory and helper methods to construct Result<TValue> instances. NOTE: This struct is not intended to be instantiated; all members are static.
public readonly struct Result
- Inherited Members
- Extension Methods
Methods
Failure(Error)
Creates a failed unit result with the specified error.
public static Result<Unit> Failure(Error error)
Parameters
errorErrorError describing the failure.
Returns
- Result<Unit>
A failed Result<TValue> of Unit.
FailureIfAsync<TValue>(Func<Task<bool>>, TValue, Error)
Asynchronously determines failure/success using failurePredicate (inverse semantics of SuccessIfAsync<TValue>(Func<Task<bool>>, TValue, Error)).
public static Task<Result<TValue>> FailureIfAsync<TValue>(Func<Task<bool>> failurePredicate, TValue value, Error error)
Parameters
failurePredicateFunc<Task<bool>>Async predicate producing true for failure.
valueTValueSuccess value if predicate is false.
errorErrorError if predicate is true.
Returns
- Task<Result<TValue>>
A task producing a success or failure Result<TValue>.
Type Parameters
TValueType of the value.
FailureIf<TValue>(bool, TValue, Error)
Returns failure if isFailure is true; otherwise success with value.
public static Result<TValue> FailureIf<TValue>(bool isFailure, TValue value, Error error)
Parameters
isFailureboolIf true produce a failure result.
valueTValueSuccess value when not failing.
errorErrorError when failing.
Returns
- Result<TValue>
A success or failure Result<TValue>.
Type Parameters
TValueType of the value.
FailureIf<TValue>(Func<bool>, in TValue, Error)
Returns failure if the provided predicate returns true; otherwise success with value.
public static Result<TValue> FailureIf<TValue>(Func<bool> failurePredicate, in TValue value, Error error)
Parameters
failurePredicateFunc<bool>Predicate indicating a failure condition.
valueTValueSuccess value when predicate is false.
errorErrorError when predicate is true.
Returns
- Result<TValue>
A success or failure Result<TValue>.
Type Parameters
TValueType of the value.
Failure<TValue>(Error)
Creates a failed result with the specified error.
public static Result<TValue> Failure<TValue>(Error error)
Parameters
errorErrorError describing the failure.
Returns
- Result<TValue>
A failed Result<TValue>.
Type Parameters
TValueType of the (missing) success value.
Failure<TValue>(Func<Error>)
Creates a failed result using a deferred error factory.
public static Result<TValue> Failure<TValue>(Func<Error> error)
Parameters
Returns
- Result<TValue>
A failed Result<TValue>.
Type Parameters
TValueType of the (missing) success value.
FromException(Exception, Func<Exception, Error>?)
Converts an exception to a failed unit result using the optional mapper (default Unexpected).
public static Result<Unit> FromException(Exception ex, Func<Exception, Error>? map = null)
Parameters
Returns
FromException<T>(Exception, Func<Exception, Error>?)
Converts an exception to a failed result of type T using the optional mapper (default Unexpected).
public static Result<T> FromException<T>(Exception ex, Func<Exception, Error>? map = null)
Parameters
Returns
- Result<T>
A failed result.
Type Parameters
TType parameter of the target result.
Success()
Creates a successful unit result (no payload).
public static Result<Unit> Success()
Returns
- Result<Unit>
A successful Result<TValue> of Unit.
SuccessIfAsync<TValue>(Func<Task<bool>>, TValue, Error)
Asynchronously determines success/failure using predicate.
public static Task<Result<TValue>> SuccessIfAsync<TValue>(Func<Task<bool>> predicate, TValue value, Error error)
Parameters
predicateFunc<Task<bool>>Async predicate producing true for success.
valueTValueSuccess value if predicate is true.
errorErrorError if predicate is false.
Returns
- Task<Result<TValue>>
A task producing a success or failure Result<TValue>.
Type Parameters
TValueType of the success value.
SuccessIf<TValue>(bool, in TValue, Error)
Returns a success or failure result based on isSuccess.
public static Result<TValue> SuccessIf<TValue>(bool isSuccess, in TValue value, Error error)
Parameters
isSuccessboolIf true returns success; otherwise failure.
valueTValueValue for the success case.
errorErrorError for the failure case.
Returns
- Result<TValue>
A success or failure Result<TValue>.
Type Parameters
TValueType of the success value.
SuccessIf<T1, T2>(bool, in T1, in T2, Error)
Returns a success (tuple) or failure result based on isSuccess.
public static Result<(T1, T2)> SuccessIf<T1, T2>(bool isSuccess, in T1 t1, in T2 t2, Error error)
Parameters
isSuccessboolIf true returns success; otherwise failure.
t1T1First value for the success case.
t2T2Second value for the success case.
errorErrorError for the failure case.
Returns
- Result<(T1, T2)>
A success or failure Result<TValue> with a tuple payload.
Type Parameters
T1Type of first value.
T2Type of second value.
Success<TValue>(Func<TValue>)
Creates a successful result by invoking the supplied factory function.
public static Result<TValue> Success<TValue>(Func<TValue> funcOk)
Parameters
funcOkFunc<TValue>Factory function producing the value. Must not be null.
Returns
- Result<TValue>
A successful Result<TValue> containing the produced value.
Type Parameters
TValueType of the success value.
Exceptions
- ArgumentNullException
Thrown if
funcOkis null.
Success<TValue>(TValue)
Creates a successful result wrapping the provided value.
public static Result<TValue> Success<TValue>(TValue value)
Parameters
valueTValueValue to wrap in a successful result (may be null for reference types).
Returns
- Result<TValue>
A successful Result<TValue> containing
value.
Type Parameters
TValueType of the success value.
TryAsync<T>(Func<Task<T>>, Func<Exception, Error>?)
Executes the asynchronous function and converts exceptions to a failed result using the optional mapper (default maps to Unexpected).
public static Task<Result<T>> TryAsync<T>(Func<Task<T>> func, Func<Exception, Error>? map = null)
Parameters
funcFunc<Task<T>>Asynchronous function to execute.
mapFunc<Exception, Error>Optional exception-to-error mapper. If null, a default Unexpected error is used.
Returns
Type Parameters
TType of the produced value.
Try<T>(Func<T>, Func<Exception, Error>?)
Executes the function and converts exceptions to a failed result using the optional mapper (default maps to Unexpected(string, string, string?)).
public static Result<T> Try<T>(Func<T> func, Func<Exception, Error>? map = null)
Parameters
funcFunc<T>Function to execute.
mapFunc<Exception, Error>Optional exception-to-error mapper. If null, a default Unexpected error is used.
Returns
- Result<T>
A success result with the value or a failure result if an exception was thrown.
Type Parameters
TType of the produced value.