| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Results; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents the framework-neutral outcome of an operation. |
| | | 7 | | /// </summary> |
| | | 8 | | public class OperationResult |
| | | 9 | | { |
| | | 10 | | private const string DefaultFailureCode = "operation.failed"; |
| | | 11 | | private const string DefaultFailureMessage = "Operation failed."; |
| | | 12 | | |
| | 5 | 13 | | private static readonly IReadOnlyList<OperationReason> EmptyReasons = |
| | 5 | 14 | | Array.AsReadOnly(Array.Empty<OperationReason>()); |
| | | 15 | | |
| | 5 | 16 | | private static readonly IReadOnlyList<string> EmptyWarnings = |
| | 5 | 17 | | Array.AsReadOnly(Array.Empty<string>()); |
| | | 18 | | |
| | 5 | 19 | | private static readonly ReadOnlyCollection<string> EmptyReasonCodes = |
| | 5 | 20 | | Array.AsReadOnly(Array.Empty<string>()); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="OperationResult"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="succeeded">A value indicating whether the operation succeeded.</param> |
| | | 26 | | /// <param name="reasons">The reasons associated with the operation result.</param> |
| | | 27 | | /// <param name="warnings">The warnings associated with the operation result.</param> |
| | 89 | 28 | | protected OperationResult( |
| | 89 | 29 | | bool succeeded, |
| | 89 | 30 | | IReadOnlyList<OperationReason> reasons, |
| | 89 | 31 | | IReadOnlyList<string> warnings) |
| | | 32 | | { |
| | 89 | 33 | | Succeeded = succeeded; |
| | 89 | 34 | | Reasons = reasons; |
| | 89 | 35 | | Warnings = warnings; |
| | 89 | 36 | | ReasonCodes = CreateReasonCodes(reasons); |
| | 89 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets a value indicating whether the operation succeeded. |
| | | 41 | | /// </summary> |
| | 84 | 42 | | public bool Succeeded { get; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets a value indicating whether the operation failed. |
| | | 46 | | /// </summary> |
| | 8 | 47 | | public bool Failed => !Succeeded; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the reasons associated with the operation result. |
| | | 51 | | /// </summary> |
| | 18 | 52 | | public IReadOnlyList<OperationReason> Reasons { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets machine-readable reason codes associated with the operation result. |
| | | 56 | | /// </summary> |
| | 42 | 57 | | public IReadOnlyList<string> ReasonCodes { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets human-readable warning messages associated with the operation result. |
| | | 61 | | /// </summary> |
| | 28 | 62 | | public IReadOnlyList<string> Warnings { get; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets a value indicating whether the operation result has warning messages. |
| | | 66 | | /// </summary> |
| | 15 | 67 | | public bool HasWarnings => Warnings.Count > 0; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Creates a successful operation result. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <returns>A successful operation result.</returns> |
| | | 73 | | public static OperationResult Success() |
| | | 74 | | { |
| | 12 | 75 | | return new OperationResult(true, EmptyReasons, EmptyWarnings); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Creates a successful operation result with warnings. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="warnings">The warnings associated with the successful result.</param> |
| | | 82 | | /// <returns>A successful operation result.</returns> |
| | | 83 | | public static OperationResult Success(IEnumerable<string> warnings) |
| | | 84 | | { |
| | 5 | 85 | | return new OperationResult(true, EmptyReasons, NormalizeWarnings(warnings)); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Creates a successful operation result with a value. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <typeparam name="TValue">The result value type.</typeparam> |
| | | 92 | | /// <param name="value">The operation value.</param> |
| | | 93 | | /// <returns>A successful operation result.</returns> |
| | | 94 | | public static OperationResult<TValue> Success<TValue>(TValue value) |
| | | 95 | | { |
| | 27 | 96 | | return new OperationResult<TValue>(value, EmptyReasons, EmptyWarnings); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Creates a successful operation result with a value and warnings. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <typeparam name="TValue">The result value type.</typeparam> |
| | | 103 | | /// <param name="value">The operation value.</param> |
| | | 104 | | /// <param name="warnings">The warnings associated with the successful result.</param> |
| | | 105 | | /// <returns>A successful operation result.</returns> |
| | | 106 | | public static OperationResult<TValue> Success<TValue>(TValue value, IEnumerable<string> warnings) |
| | | 107 | | { |
| | 1 | 108 | | return new OperationResult<TValue>(value, EmptyReasons, NormalizeWarnings(warnings)); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Creates a failed operation result. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="code">The machine-readable reason code.</param> |
| | | 115 | | /// <param name="message">The human-readable reason message.</param> |
| | | 116 | | /// <returns>A failed operation result.</returns> |
| | | 117 | | public static OperationResult Failure(string code, string message) |
| | | 118 | | { |
| | 27 | 119 | | return Failure(OperationReason.Create(code, message)); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Creates a failed operation result. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <typeparam name="TValue">The result value type.</typeparam> |
| | | 126 | | /// <param name="code">The machine-readable reason code.</param> |
| | | 127 | | /// <param name="message">The human-readable reason message.</param> |
| | | 128 | | /// <returns>A failed operation result.</returns> |
| | | 129 | | public static OperationResult<TValue> Failure<TValue>(string code, string message) |
| | | 130 | | { |
| | 4 | 131 | | return Failure<TValue>(OperationReason.Create(code, message)); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Creates a failed operation result. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="reason">The reason associated with the failed result.</param> |
| | | 138 | | /// <returns>A failed operation result.</returns> |
| | | 139 | | public static OperationResult Failure(OperationReason reason) |
| | | 140 | | { |
| | 28 | 141 | | ArgumentNullException.ThrowIfNull(reason); |
| | | 142 | | |
| | 27 | 143 | | return new OperationResult(false, Array.AsReadOnly([reason]), EmptyWarnings); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Creates a failed operation result. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <typeparam name="TValue">The result value type.</typeparam> |
| | | 150 | | /// <param name="reason">The reason associated with the failed result.</param> |
| | | 151 | | /// <returns>A failed operation result.</returns> |
| | | 152 | | public static OperationResult<TValue> Failure<TValue>(OperationReason reason) |
| | | 153 | | { |
| | 5 | 154 | | ArgumentNullException.ThrowIfNull(reason); |
| | | 155 | | |
| | 4 | 156 | | return new OperationResult<TValue>(Array.AsReadOnly([reason]), EmptyWarnings); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Creates a failed operation result. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <param name="reasons">The reasons associated with the failed result.</param> |
| | | 163 | | /// <returns>A failed operation result.</returns> |
| | | 164 | | public static OperationResult Failure(IEnumerable<OperationReason> reasons) |
| | | 165 | | { |
| | 7 | 166 | | return new OperationResult(false, NormalizeReasons(reasons), EmptyWarnings); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Creates a failed operation result. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <typeparam name="TValue">The result value type.</typeparam> |
| | | 173 | | /// <param name="reasons">The reasons associated with the failed result.</param> |
| | | 174 | | /// <returns>A failed operation result.</returns> |
| | | 175 | | public static OperationResult<TValue> Failure<TValue>(IEnumerable<OperationReason> reasons) |
| | | 176 | | { |
| | 2 | 177 | | return new OperationResult<TValue>(NormalizeReasons(reasons), EmptyWarnings); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Creates a failed operation result with warnings. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="reasons">The reasons associated with the failed result.</param> |
| | | 184 | | /// <param name="warnings">The warnings associated with the failed result.</param> |
| | | 185 | | /// <returns>A failed operation result.</returns> |
| | | 186 | | public static OperationResult Failure( |
| | | 187 | | IEnumerable<OperationReason> reasons, |
| | | 188 | | IEnumerable<string> warnings) |
| | | 189 | | { |
| | 1 | 190 | | return new OperationResult(false, NormalizeReasons(reasons), NormalizeWarnings(warnings)); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Creates a failed operation result with warnings. |
| | | 195 | | /// </summary> |
| | | 196 | | /// <typeparam name="TValue">The result value type.</typeparam> |
| | | 197 | | /// <param name="reasons">The reasons associated with the failed result.</param> |
| | | 198 | | /// <param name="warnings">The warnings associated with the failed result.</param> |
| | | 199 | | /// <returns>A failed operation result.</returns> |
| | | 200 | | public static OperationResult<TValue> Failure<TValue>( |
| | | 201 | | IEnumerable<OperationReason> reasons, |
| | | 202 | | IEnumerable<string> warnings) |
| | | 203 | | { |
| | 1 | 204 | | return new OperationResult<TValue>(NormalizeReasons(reasons), NormalizeWarnings(warnings)); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Creates a failed operation result using the default failure reason. |
| | | 209 | | /// </summary> |
| | | 210 | | /// <returns>A failed operation result.</returns> |
| | | 211 | | public static OperationResult Failure() |
| | | 212 | | { |
| | 1 | 213 | | return Failure(DefaultFailureCode, DefaultFailureMessage); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Creates a failed operation result using the default failure reason. |
| | | 218 | | /// </summary> |
| | | 219 | | /// <typeparam name="TValue">The result value type.</typeparam> |
| | | 220 | | /// <returns>A failed operation result.</returns> |
| | | 221 | | public static OperationResult<TValue> Failure<TValue>() |
| | | 222 | | { |
| | 2 | 223 | | return new OperationResult<TValue>(NormalizeReasons(null), EmptyWarnings); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Creates a failed operation result from this result's reasons and warnings. |
| | | 228 | | /// </summary> |
| | | 229 | | /// <returns>A failed operation result.</returns> |
| | | 230 | | public OperationResult ToFailure() |
| | | 231 | | { |
| | 2 | 232 | | return Failed |
| | 2 | 233 | | ? this |
| | 2 | 234 | | : Failure(); |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Normalizes operation reasons and provides a default failure reason when none are supplied. |
| | | 239 | | /// </summary> |
| | | 240 | | /// <param name="reasons">The reasons to normalize.</param> |
| | | 241 | | /// <returns>A normalized reason collection.</returns> |
| | | 242 | | protected static IReadOnlyList<OperationReason> NormalizeReasons(IEnumerable<OperationReason>? reasons) |
| | | 243 | | { |
| | 13 | 244 | | return NormalizeReasons(reasons, DefaultFailureCode, DefaultFailureMessage); |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | private static ReadOnlyCollection<OperationReason> NormalizeReasons( |
| | | 248 | | IEnumerable<OperationReason>? reasons, |
| | | 249 | | string fallbackCode, |
| | | 250 | | string fallbackMessage) |
| | | 251 | | { |
| | 13 | 252 | | if (reasons is null) |
| | | 253 | | { |
| | 3 | 254 | | return CreateFallbackReason(fallbackCode, fallbackMessage); |
| | | 255 | | } |
| | | 256 | | |
| | 10 | 257 | | if (reasons is ICollection<OperationReason> collection) |
| | | 258 | | { |
| | 8 | 259 | | if (collection.Count == 0) |
| | | 260 | | { |
| | 2 | 261 | | return CreateFallbackReason(fallbackCode, fallbackMessage); |
| | | 262 | | } |
| | | 263 | | |
| | 6 | 264 | | var normalizedReasons = new OperationReason[collection.Count]; |
| | 6 | 265 | | int normalizedCount = 0; |
| | | 266 | | |
| | 30 | 267 | | foreach (OperationReason? reason in collection) |
| | | 268 | | { |
| | 9 | 269 | | if (reason is not null) |
| | | 270 | | { |
| | 7 | 271 | | normalizedReasons[normalizedCount] = reason; |
| | 7 | 272 | | normalizedCount++; |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | |
| | 6 | 276 | | if (normalizedCount == 0) |
| | | 277 | | { |
| | 1 | 278 | | return CreateFallbackReason(fallbackCode, fallbackMessage); |
| | | 279 | | } |
| | | 280 | | |
| | 5 | 281 | | if (normalizedCount == normalizedReasons.Length) |
| | | 282 | | { |
| | 4 | 283 | | return Array.AsReadOnly(normalizedReasons); |
| | | 284 | | } |
| | | 285 | | |
| | 1 | 286 | | var filteredReasons = new OperationReason[normalizedCount]; |
| | 1 | 287 | | Array.Copy(normalizedReasons, filteredReasons, normalizedCount); |
| | | 288 | | |
| | 1 | 289 | | return Array.AsReadOnly(filteredReasons); |
| | | 290 | | } |
| | | 291 | | |
| | 2 | 292 | | List<OperationReason>? normalizedList = null; |
| | | 293 | | |
| | 8 | 294 | | foreach (OperationReason? reason in reasons) |
| | | 295 | | { |
| | 2 | 296 | | if (reason is not null) |
| | | 297 | | { |
| | 2 | 298 | | normalizedList ??= []; |
| | 2 | 299 | | normalizedList.Add(reason); |
| | | 300 | | } |
| | | 301 | | } |
| | | 302 | | |
| | 2 | 303 | | return normalizedList is null || normalizedList.Count == 0 |
| | 2 | 304 | | ? CreateFallbackReason(fallbackCode, fallbackMessage) |
| | 2 | 305 | | : Array.AsReadOnly([.. normalizedList]); |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | private static ReadOnlyCollection<OperationReason> CreateFallbackReason( |
| | | 309 | | string fallbackCode, |
| | | 310 | | string fallbackMessage) |
| | | 311 | | { |
| | 7 | 312 | | return Array.AsReadOnly([OperationReason.Create(fallbackCode, fallbackMessage)]); |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Normalizes warning messages. |
| | | 317 | | /// </summary> |
| | | 318 | | /// <param name="warnings">The warnings to normalize.</param> |
| | | 319 | | /// <returns>A normalized warning collection.</returns> |
| | | 320 | | protected static IReadOnlyList<string> NormalizeWarnings(IEnumerable<string>? warnings) |
| | | 321 | | { |
| | 8 | 322 | | string[] normalizedWarnings = warnings? |
| | 12 | 323 | | .Where(warning => !string.IsNullOrWhiteSpace(warning)) |
| | 9 | 324 | | .Select(warning => warning.Trim()) |
| | 8 | 325 | | .ToArray() ?? []; |
| | | 326 | | |
| | 8 | 327 | | return normalizedWarnings.Length == 0 |
| | 8 | 328 | | ? EmptyWarnings |
| | 8 | 329 | | : Array.AsReadOnly(normalizedWarnings); |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | private static ReadOnlyCollection<string> CreateReasonCodes(IReadOnlyList<OperationReason> reasons) |
| | | 333 | | { |
| | 89 | 334 | | if (reasons.Count == 0) |
| | | 335 | | { |
| | 45 | 336 | | return EmptyReasonCodes; |
| | | 337 | | } |
| | | 338 | | |
| | 44 | 339 | | string[] reasonCodes = new string[reasons.Count]; |
| | | 340 | | |
| | 182 | 341 | | for (int index = 0; index < reasons.Count; index++) |
| | | 342 | | { |
| | 47 | 343 | | reasonCodes[index] = reasons[index].Code; |
| | | 344 | | } |
| | | 345 | | |
| | 44 | 346 | | return Array.AsReadOnly(reasonCodes); |
| | | 347 | | } |
| | | 348 | | } |