| | | 1 | | namespace Elsa.Common.Models; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// A strongly typed monad that runs either the <see cref="OnSuccess"/> or <see cref="OnFailure"/> lambda, depending on |
| | | 5 | | /// </summary> |
| | 23371 | 6 | | public class Result<T>(bool success, T? value, Exception? exception) |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// True if the conversion succeeded, false otherwise. |
| | | 10 | | /// </summary> |
| | 93742 | 11 | | public bool IsSuccess { get; } = success; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// The result value. Throws an exception if accessed when the result is a failure. |
| | | 15 | | /// </summary> |
| | | 16 | | public T Value |
| | | 17 | | { |
| | | 18 | | get |
| | | 19 | | { |
| | 26059 | 20 | | if (!IsSuccess) |
| | 0 | 21 | | throw new InvalidOperationException("Cannot access Value on a failed result. Check IsSuccess first or us |
| | 26059 | 22 | | return value!; |
| | | 23 | | } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The result value, or null/default if the result is a failure. Useful when null is a valid success value. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public T? ValueOrDefault => value; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Any exception that may have occurred during the operation. |
| | | 33 | | /// </summary> |
| | 25786 | 34 | | public Exception? Exception { get; } = exception; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Runs the provided delegate if the result is successful. |
| | | 38 | | /// </summary> |
| | | 39 | | public Result<T> OnSuccess(Action<T> successHandler) |
| | | 40 | | { |
| | 5103 | 41 | | if (IsSuccess) |
| | 5103 | 42 | | successHandler(Value); |
| | | 43 | | |
| | 5103 | 44 | | return this; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Runs the provided async delegate if the result is successful. |
| | | 49 | | /// </summary> |
| | | 50 | | public async Task<Result<T>> OnSuccessAsync(Func<T, Task> successHandler) |
| | | 51 | | { |
| | 2688 | 52 | | if (IsSuccess) |
| | 2688 | 53 | | await successHandler(Value); |
| | | 54 | | |
| | 2688 | 55 | | return this; |
| | 2688 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Runs the provided delegate if the result is unsuccessful. |
| | | 60 | | /// </summary> |
| | | 61 | | public Result<T> OnFailure(Action<Exception> failureHandler) |
| | | 62 | | { |
| | 2415 | 63 | | if (Exception != null) |
| | 0 | 64 | | failureHandler(Exception); |
| | | 65 | | |
| | 2415 | 66 | | return this; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Runs the provided async delegate if the result is unsuccessful. |
| | | 71 | | /// </summary> |
| | | 72 | | public async Task<Result<T>> OnFailureAsync(Func<Exception, Task> failureHandler) |
| | | 73 | | { |
| | 0 | 74 | | if (Exception != null) |
| | 0 | 75 | | await failureHandler(Exception); |
| | | 76 | | |
| | 0 | 77 | | return this; |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Throws the exception if the result is a failure. |
| | | 82 | | /// </summary> |
| | | 83 | | public Result<T> ThrowIfFailure() |
| | | 84 | | { |
| | 0 | 85 | | if (!IsSuccess && Exception != null) |
| | 0 | 86 | | throw Exception; |
| | | 87 | | |
| | 0 | 88 | | return this; |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// A simple monad that runs either the <see cref="Result{T}.OnSuccess"/> or <see cref="Result{T}.OnFailure"/> lambda, d |
| | | 94 | | /// </summary> |
| | | 95 | | public class Result(bool success, object? value, Exception? exception) : Result<object>(success, value, exception) |
| | | 96 | | { |
| | | 97 | | /// <summary> |
| | | 98 | | /// Creates a successful result with the specified value. |
| | | 99 | | /// </summary> |
| | | 100 | | public static Result<T> Success<T>(T value) => new(true, value, null); |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Creates a failed result with the specified exception. |
| | | 104 | | /// </summary> |
| | | 105 | | public static Result<T> Failure<T>(Exception exception) => new(false, default, exception); |
| | | 106 | | } |