|
| 1 | +// Copyright (c) Winton. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace Winton.DomainModelling |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Extension methods for asynchronous results which make it possible to chain async results together |
| 11 | + /// using a fluent API in the same way as synchronous results. |
| 12 | + /// </summary> |
| 13 | + public static class AsyncResultExtensions |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Invokes another result generating function which takes as input the data of this result |
| 17 | + /// if it is successful after it has been awaited. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// If this result is a failure then this is a no-op and the original failure is retained. |
| 21 | + /// This is useful for chaining serial operations together that return results. |
| 22 | + /// </remarks> |
| 23 | + /// <typeparam name="TData"> |
| 24 | + /// The type of data encapsulated by the result. |
| 25 | + /// </typeparam> |
| 26 | + /// <typeparam name="TNewData"> |
| 27 | + /// The type of data in the new result. |
| 28 | + /// </typeparam> |
| 29 | + /// <param name="resultTask"> |
| 30 | + /// The async result that this extension method is invoked on. |
| 31 | + /// </param> |
| 32 | + /// <param name="onSuccess"> |
| 33 | + /// The function that is invoked if this result represents a success. |
| 34 | + /// </param> |
| 35 | + /// <returns> |
| 36 | + /// If this result is a success, then the result of <paramref>onSuccess</paramref> function; |
| 37 | + /// otherwise the original error. |
| 38 | + /// </returns> |
| 39 | + public static async Task<Result<TNewData>> Then<TData, TNewData>( |
| 40 | + this Task<Result<TData>> resultTask, |
| 41 | + Func<TData, Result<TNewData>> onSuccess) |
| 42 | + { |
| 43 | + Result<TData> result = await resultTask; |
| 44 | + return result.Then(onSuccess); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Invokes another result generating function which takes as input the data of this result |
| 49 | + /// if it is successful after it has been awaited. |
| 50 | + /// </summary> |
| 51 | + /// <remarks> |
| 52 | + /// If this result is a failure then this is a no-op and the original failure is retained. |
| 53 | + /// This is useful for chaining serial operations together that return results. |
| 54 | + /// </remarks> |
| 55 | + /// <typeparam name="TData"> |
| 56 | + /// The type of data encapsulated by the result. |
| 57 | + /// </typeparam> |
| 58 | + /// <typeparam name="TNewData"> |
| 59 | + /// The type of data in the new result. |
| 60 | + /// </typeparam> |
| 61 | + /// <param name="resultTask"> |
| 62 | + /// The async result that this extension method is invoked on. |
| 63 | + /// </param> |
| 64 | + /// <param name="onSuccess"> |
| 65 | + /// The asynchronous function that is invoked if this result represents a success. |
| 66 | + /// </param> |
| 67 | + /// <returns> |
| 68 | + /// If this result is a success, then the result of <paramref>onSuccess</paramref> function; |
| 69 | + /// otherwise the original error. |
| 70 | + /// </returns> |
| 71 | + public static async Task<Result<TNewData>> Then<TData, TNewData>( |
| 72 | + this Task<Result<TData>> resultTask, |
| 73 | + Func<TData, Task<Result<TNewData>>> onSuccess) |
| 74 | + { |
| 75 | + Result<TData> result = await resultTask; |
| 76 | + return await result.Then(onSuccess); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments