public static IOperationResult <TOut> Bind <TIn, TOut>(this IOperationResult <TIn> source, Func <TIn, IOperationResult <TOut> > bind) { return(source.Match( value => bind(value), error => Error <TOut>(error), exception => Failure <TOut>(exception) )); }
/// <summary> /// Returns the new operation result using the <c>returnResult</c> function if the source operation result doesn't match as a success. /// Otherwise the operation result is passed on unchanged. /// </summary> /// <param name="source">The operation result to be matched.</param> /// <param name="returnResult">The function used as an alternative way of getting a success if the source result doesn't match as one.</param> /// <typeparam name="T">The type of the expected result value.</typeparam> /// <returns>A new operation result calculated based on the matching of the source result.</returns> public static IOperationResult <T> IfError <T>(this IOperationResult <T> source, Func <IOperationResult <T> > returnResult) { return(source.Match( _ => source, (_, __) => returnResult(), _ => returnResult() )); }
/// <summary> /// Applies the <c>bind</c> function to the message of the given operation result if it doesn't match as a success. /// Otherwise the given operation result is passed on with the appropriate expected value type. /// </summary> /// <param name="source">The operation result to be processed.</param> /// <param name="bind">The function to be applied to the source result error message and code if it matches as an error or to the exception message and the standard failure code if it matches as a failure.</param> /// <param name="getFailureMessage">A function used to extract the exception message from the result if it matches as a failure.</param> /// <param name="systemFailureCode">The standard code used for system failures.</param> /// <typeparam name="T">The expected type of the source result value.</typeparam> /// <returns>A new operation result calculated based on the matching of the source result.</returns> public static IOperationResult <T> BindError <T>(this IOperationResult <T> source, Func <int?, string, IOperationResult <T> > bind, Func <Exception, string> getFailureMessage, int?systemFailureCode = null) { return(source.Match( _ => source, (code, error) => bind(code, error), failure => bind(systemFailureCode, getFailureMessage(failure)) )); }
/// <summary> /// Uses the value of the given operation result if it matches as a success to execute the <c>use</c> side effect. /// The source operation result is passed on unchanged. /// </summary> /// <param name="source">The operation result to be processed.</param> /// <param name="use">The side effect to be executed for the source result value if it matches as a success.</param> /// <typeparam name="T">The expected type of the source result value.</typeparam> /// <returns>The source operation result.</returns> public static IOperationResult <T> Use <T>(this IOperationResult <T> source, Action <T> use) { return(source.Match( success => { use(success); return source; }, (_, __) => source, _ => source)); }
/// <summary> /// Uses the message of the given operation result if it doesn't match as a success to execute the <c>use</c> side effect. /// The source operation result is passed on unchanged. /// </summary> /// <param name="source">The operation result to be processed.</param> /// <param name="use">The side effect to be executed for the source result error code and message if it matches as an error or for the exception message and system failure code if it matches as a failure.</param> /// <param name="getFailureMessage">A function used to extract the exception message from the result if it matches as a failure.</param> /// <param name="systemFailureCode">The standard code used for system failures.</param> /// <typeparam name="T">The expected type of the source result value.</typeparam> /// <returns>The source operation result.</returns> public static IOperationResult <T> UseError <T>(this IOperationResult <T> source, Action <int?, string> use, Func <Exception, string> getFailureMessage, int?systemFailureCode = null) { return(source.Match( _ => source, (code, error) => { use(code, error); return source; }, failure => { use(systemFailureCode, getFailureMessage(failure)); return source; } )); }
/// <summary> /// Uses the message of the given operation result if it doesn't match as a success to execute the <c>use</c> side effect. /// The source operation result is passed on unchanged. /// </summary> /// <param name="source">The operation result to be processed.</param> /// <param name="use">The side effect to be executed for the source result error message if it matches as an error or for the exception message if it matches as a failure.</param> /// <param name="getFailureMessage">A function used to extract the exception message from the result if it matches as a failure.</param> /// <typeparam name="T">The expected type of the source result value.</typeparam> /// <returns>The source operation result.</returns> public static IOperationResult <T> UseError <T>(this IOperationResult <T> source, Action <string> use, Func <Exception, string> getFailureMessage) { return(source.Match( _ => source, (code, error) => { use(error); return source; }, failure => { use(getFailureMessage(failure)); return source; } )); }