您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Diagnostics;
  3. namespace iiie.Logs.DBO
  4. {
  5. /// <summary>
  6. /// Represent the result of an operation, with details in case of failure and data in case of success
  7. /// </summary>
  8. /// <typeparam name="T">The data type</typeparam>
  9. public class OpResult<T>
  10. {
  11. /// <summary>
  12. /// Status of the operation
  13. /// </summary>
  14. public ResultStatus Status { get; set; }
  15. /// <summary>
  16. /// True if Status is Success, false otherwise
  17. /// </summary>
  18. public bool Success
  19. {
  20. get
  21. {
  22. return Status == ResultStatus.Success;
  23. }
  24. }
  25. /// <summary>
  26. /// Details on the error, or null if error details must not be shown to the user
  27. /// Only relevent when Success is false
  28. /// </summary>
  29. public string PublicDetails { get; set; }
  30. /// <summary>
  31. /// Technical details on the error
  32. /// Only relevent when Success is false
  33. /// </summary>
  34. public string PrivateDetails { get; set; }
  35. public StackFrame[] StackFrames { get; set; }
  36. /// <summary>
  37. /// The data returned by the call
  38. /// Only relevent if Success is true
  39. /// </summary>
  40. public T Data { get; set; }
  41. /// <summary>
  42. /// Create and returns a new OpResult with
  43. /// {Public,Private}Details = null, Data = data, Status = true
  44. /// Used for successfull operations
  45. /// </summary>
  46. public static OpResult<T> Ok(T data)
  47. {
  48. return new OpResult<T>
  49. {
  50. PublicDetails = null,
  51. PrivateDetails = null,
  52. StackFrames = new StackTrace().GetFrames(),
  53. Data = data,
  54. Status = ResultStatus.Success
  55. };
  56. }
  57. /// <summary>
  58. /// Create and returns a new OpResult with
  59. /// PrivateDetails = privateDetails, Data = null, Status = status
  60. /// If publicDetails == "", privateDetails will be used
  61. /// Used for all errors (invalid input, ...)
  62. /// </summary>
  63. public static OpResult<T> Error(ResultStatus status, string privateDetails, string publicDetails = null)
  64. {
  65. return new OpResult<T>
  66. {
  67. PublicDetails = publicDetails == "" ? privateDetails : publicDetails,
  68. PrivateDetails = privateDetails,
  69. StackFrames = new StackTrace().GetFrames(),
  70. Data = default(T),
  71. Status = status
  72. };
  73. }
  74. /// <summary>
  75. /// Create and returns a new OpResult with
  76. /// PrivateDetails = e to string, Data = null, Status = status
  77. /// If publicDetails == "", privateDetails will be used
  78. /// Used for all errors (invalid input, ...)
  79. /// </summary>
  80. public static OpResult<T> Error(ResultStatus status, Exception e, string userDetails = null)
  81. {
  82. string message = "";
  83. Exception inner = e;
  84. while (inner != null)
  85. {
  86. message += inner.GetType().Name + ": " + inner.Message + (inner.InnerException != null ? "\n" : "");
  87. inner = inner.InnerException;
  88. }
  89. return Error(status, message, userDetails);
  90. }
  91. /// <summary>
  92. /// Translate an error to another type
  93. /// </summary>
  94. /// <typeparam name="T2">The original type</typeparam>
  95. /// <param name="other">The original error</param>
  96. /// <returns>The original error with the type T</returns>
  97. public static OpResult<T> Error<T2>(OpResult<T2> other)
  98. {
  99. return new OpResult<T>
  100. {
  101. PublicDetails = other.PublicDetails,
  102. PrivateDetails = other.PrivateDetails,
  103. StackFrames = other.StackFrames,
  104. Data = default(T),
  105. Status = other.Status
  106. };
  107. }
  108. /// <summary>
  109. /// Translate an error to another type
  110. /// </summary>
  111. /// <typeparam name="T2">The original type</typeparam>
  112. /// <returns>The original error with the type T</returns>
  113. public OpResult<T2> To<T2>()
  114. {
  115. return OpResult<T2>.Error(this);
  116. }
  117. /// <summary>
  118. /// Implicit converion to bool
  119. /// </summary>
  120. /// <param name="res">The result to be converted</param>
  121. /// <returns>True if Success, false otherwise</returns>
  122. public static implicit operator bool(OpResult<T> res)
  123. {
  124. return res.Success;
  125. }
  126. protected OpResult()
  127. {
  128. }
  129. }
  130. }