< Summary

Information
Class: Elsa.Common.Models.Result<T>
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Models/Result.cs
Line coverage
58%
Covered lines: 14
Uncovered lines: 10
Coverable lines: 24
Total lines: 106
Line coverage: 58.3%
Branch coverage
42%
Covered branches: 6
Total branches: 14
Branch coverage: 42.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_IsSuccess()100%11100%
get_Value()50%2266.66%
get_ValueOrDefault()100%210%
get_Exception()100%11100%
OnSuccess(...)100%22100%
OnSuccessAsync()100%22100%
OnFailure(...)50%2266.66%
OnFailureAsync()0%620%
ThrowIfFailure()0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Models/Result.cs

#LineLine coverage
 1namespace 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>
 233716public class Result<T>(bool success, T? value, Exception? exception)
 7{
 8    /// <summary>
 9    /// True if the conversion succeeded, false otherwise.
 10    /// </summary>
 9374211    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        {
 2605920            if (!IsSuccess)
 021                throw new InvalidOperationException("Cannot access Value on a failed result. Check IsSuccess first or us
 2605922            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>
 029    public T? ValueOrDefault => value;
 30
 31    /// <summary>
 32    /// Any exception that may have occurred during the operation.
 33    /// </summary>
 2578634    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    {
 510341        if (IsSuccess)
 510342            successHandler(Value);
 43
 510344        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    {
 268852        if (IsSuccess)
 268853            await successHandler(Value);
 54
 268855        return this;
 268856    }
 57
 58    /// <summary>
 59    /// Runs the provided delegate if the result is unsuccessful.
 60    /// </summary>
 61    public Result<T> OnFailure(Action<Exception> failureHandler)
 62    {
 241563        if (Exception != null)
 064            failureHandler(Exception);
 65
 241566        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    {
 074        if (Exception != null)
 075            await failureHandler(Exception);
 76
 077        return this;
 078    }
 79
 80    /// <summary>
 81    /// Throws the exception if the result is a failure.
 82    /// </summary>
 83    public Result<T> ThrowIfFailure()
 84    {
 085        if (!IsSuccess && Exception != null)
 086            throw Exception;
 87
 088        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>
 95public 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}