Result.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using Quadarax.Foundation.Core.Value.Extensions;
  4. namespace Quadarax.Foundation.Core.Data
  5. {
  6. public class Result : IResult
  7. {
  8. #region *** Properties ***
  9. public bool IsSuccess { get; protected set; }
  10. public IEnumerable<IError> Errors => _errors;
  11. public static IResult Ok => new Result();
  12. public static IResult Fail => new Result(false, new List<IError> { new Error("Operation failed", 0, ErrorLevelEnum.Error) });
  13. #endregion
  14. #region *** Private Fields ***
  15. private readonly IList<IError> _errors = new List<IError>();
  16. #endregion
  17. #region *** Constructors ***
  18. public Result() : this(true, null)
  19. {
  20. }
  21. public Result(IEnumerable<IError> errors) : this(false, errors)
  22. {
  23. }
  24. public Result(bool isSuccess, IEnumerable<IError>? errors)
  25. {
  26. errors ??= new List<IError>();
  27. IsSuccess = isSuccess;
  28. _errors.AddRange(errors);
  29. }
  30. #endregion
  31. #region *** Operations ***
  32. public void SetSuccess(bool isSuccess)
  33. {
  34. IsSuccess = isSuccess;
  35. }
  36. public void AddError(IError error)
  37. {
  38. if (error == null)
  39. throw new ArgumentNullException(nameof(error));
  40. _errors.Add(error);
  41. }
  42. public void ClearErrors()
  43. {
  44. _errors.Clear();
  45. }
  46. public object? GetValue()
  47. {
  48. return null;
  49. }
  50. #endregion
  51. }
  52. }