Esempio n. 1
0
 public static async Task <IOperationResult <TOut> > BindAsync <TIn, TOut>(this IOperationResult <TIn> source, Func <TIn, Task <IOperationResult <TOut> > > bindAsync)
 {
     return(await source.MatchAsync(
                async value => await bindAsync(value),
                error => Task.FromResult(Error <TOut>(error)),
                exception => Task.FromResult(Failure <TOut>(exception))
                ));
 }
Esempio n. 2
0
 /// <summary>
 /// Applies the <c>bindAsync</c> asynchronous function to the error 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="bindAsync">The asynchronous function to be applied to the produced 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 produced source result value.</typeparam>
 /// <returns>A task producing a new operation result calculated based on the matching of the source result.</returns>
 public static async Task <IOperationResult <T> > BindErrorAsync <T>(this IOperationResult <T> source, Func <int?, string, Task <IOperationResult <T> > > bindAsync, Func <Exception, string> getFailureMessage, int?systemFailureCode = null)
 {
     return(await source.MatchAsync(
                _ => Task.FromResult(source),
                async (code, error) => await bindAsync(code, error),
                async failure => await bindAsync(systemFailureCode, getFailureMessage(failure))
                ));
 }
Esempio n. 3
0
 /// <summary>
 /// Returns a task producing a new operation result using the <c>returnResultAsync</c> asynchronous function if the source operation result produced by the given task doesn't match as a success.
 /// Otherwise the operation result is passed on as a task result.
 /// </summary>
 /// <param name="source">The task producing the operation result to be processed.</param>
 /// <param name="returnResultAsync">The asynchronous 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 task producing a new operation result based on the matching of the source result.</returns>
 public static async Task <IOperationResult <T> > IfErrorAsync <T>(this IOperationResult <T> source, Func <Task <IOperationResult <T> > > returnResultAsync)
 {
     return(await source.MatchAsync(
                _ => Task.FromResult(source),
                async (_, __) => await returnResultAsync(),
                async _ => await returnResultAsync()
                ));
 }
Esempio n. 4
0
 /// <summary>
 /// Uses the value of the given operation result if it matches as a success to execute the asynchronous <c>useAsync</c> side effect.
 /// The source operation result is passed on as a task result.
 /// </summary>
 /// <param name="source">The operation result to be processed.</param>
 /// <param name="useAsync">The asynchronous 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>
 /// <typeparam name="T">The expected type of the source result value.</typeparam>
 /// <returns>A task returning the source result.</returns>
 public static async Task <IOperationResult <T> > UseAsync <T>(this IOperationResult <T> source, Func <T, Task> useAsync)
 {
     return(await source.MatchAsync(
                async success =>
     {
         await useAsync(success);
         return source;
     },
                (_, __) => Task.FromResult(source),
                _ => Task.FromResult(source)
                ));
 }
Esempio n. 5
0
 /// <summary>
 /// Uses the message of the given operation result if it doesn't match as a success to execute the asynchronous <c>useAsync</c> side effect.
 /// The source operation result is passed on as a task result.
 /// </summary>
 /// <param name="source">The operation result to be processed.</param>
 /// <param name="useAsync">The asynchronous 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>A task returning the source operation result.</returns>
 public static async Task <IOperationResult <T> > UseErrorAsync <T>(this IOperationResult <T> source, Func <string, Task> useAsync, Func <Exception, string> getFailureMessage)
 {
     return(await source.MatchAsync(
                _ => Task.FromResult(source),
                async (code, error) =>
     {
         await useAsync(error);
         return source;
     },
                async failure =>
     {
         await useAsync(getFailureMessage(failure));
         return source;
     }
                ));
 }