| | | 1 | | namespace AsiBackbone.Core.Results; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents the framework-neutral outcome of an operation that may return a value. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <typeparam name="TValue">The result value type.</typeparam> |
| | | 7 | | public sealed class OperationResult<TValue> : OperationResult |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new successful instance of the <see cref="OperationResult{TValue}"/> class. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="value">The operation value.</param> |
| | | 13 | | /// <param name="reasons">The reasons associated with the operation result.</param> |
| | | 14 | | /// <param name="warnings">The warnings associated with the operation result.</param> |
| | | 15 | | internal OperationResult( |
| | | 16 | | TValue value, |
| | | 17 | | IReadOnlyList<OperationReason> reasons, |
| | | 18 | | IReadOnlyList<string> warnings) |
| | 28 | 19 | | : base(true, reasons, warnings) |
| | | 20 | | { |
| | 28 | 21 | | ValueCore = value; |
| | 28 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new failed instance of the <see cref="OperationResult{TValue}"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="reasons">The reasons associated with the operation result.</param> |
| | | 28 | | /// <param name="warnings">The warnings associated with the operation result.</param> |
| | | 29 | | internal OperationResult( |
| | | 30 | | IReadOnlyList<OperationReason> reasons, |
| | | 31 | | IReadOnlyList<string> warnings) |
| | 9 | 32 | | : base(false, reasons, warnings) |
| | | 33 | | { |
| | 9 | 34 | | ValueCore = default!; |
| | 9 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the successful operation value. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <exception cref="InvalidOperationException"> |
| | | 41 | | /// Thrown when the operation result is failed and no successful value is available. |
| | | 42 | | /// </exception> |
| | 4 | 43 | | public TValue Value => Succeeded |
| | 4 | 44 | | ? ValueCore |
| | 4 | 45 | | : throw new InvalidOperationException("Cannot access the value of a failed operation result."); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets a value indicating whether a successful result value is available. |
| | | 49 | | /// </summary> |
| | 5 | 50 | | public bool HasValue => Succeeded; |
| | | 51 | | |
| | 3 | 52 | | private TValue ValueCore { get; } |
| | | 53 | | } |