選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

OpResult.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /// <summary>
  36. /// Original error stack trace
  37. /// </summary>
  38. public StackFrame[] StackFrames { get; set; }
  39. /// <summary>
  40. /// Caught exception, if any
  41. /// </summary>
  42. public Exception Exception { get; set; }
  43. /// <summary>
  44. /// The data returned by the call
  45. /// Only relevent if Success is true
  46. /// </summary>
  47. public T Data { get; set; }
  48. /// <summary>
  49. /// Create and returns a new OpResult with
  50. /// {Public,Private}Details = null, Data = data, Status = true
  51. /// Used for successfull operations
  52. /// </summary>
  53. public static OpResult<T> Ok(T data)
  54. {
  55. return new OpResult<T>
  56. {
  57. PublicDetails = null,
  58. PrivateDetails = null,
  59. StackFrames = new StackTrace().GetFrames(),
  60. Data = data,
  61. Status = ResultStatus.Success
  62. };
  63. }
  64. /// <summary>
  65. /// Create and returns a new OpResult with
  66. /// PrivateDetails = privateDetails, Data = null, Status = status
  67. /// If publicDetails == "", privateDetails will be used
  68. /// Used for all errors (invalid input, ...)
  69. /// </summary>
  70. public static OpResult<T> Error(ResultStatus status, string privateDetails, string publicDetails = null)
  71. {
  72. return new OpResult<T>
  73. {
  74. PublicDetails = publicDetails == "" ? privateDetails : publicDetails,
  75. PrivateDetails = privateDetails,
  76. StackFrames = new StackTrace().GetFrames(),
  77. Data = default(T),
  78. Status = status
  79. };
  80. }
  81. /// <summary>
  82. /// Create and returns a new OpResult with
  83. /// PrivateDetails = e to string, Data = null, Status = status
  84. /// If publicDetails == "", privateDetails will be used
  85. /// Used for all errors (invalid input, ...)
  86. /// </summary>
  87. public static OpResult<T> Error(ResultStatus status, Exception e, string userDetails = null)
  88. {
  89. string message = "";
  90. Exception inner = e;
  91. while (inner != null)
  92. {
  93. message += inner.GetType().Name + ": " + inner.Message + (inner.InnerException != null ? "\n" : "");
  94. inner = inner.InnerException;
  95. }
  96. var res = Error(status, message, userDetails);
  97. res.Exception = e;
  98. return res;
  99. }
  100. /// <summary>
  101. /// Translate an error to another type
  102. /// </summary>
  103. /// <typeparam name="T2">The original type</typeparam>
  104. /// <param name="other">The original error</param>
  105. /// <returns>The original error with the type T</returns>
  106. public static OpResult<T> Error<T2>(OpResult<T2> other)
  107. {
  108. return new OpResult<T>
  109. {
  110. PublicDetails = other.PublicDetails,
  111. PrivateDetails = other.PrivateDetails,
  112. StackFrames = other.StackFrames,
  113. Data = default(T),
  114. Status = other.Status
  115. };
  116. }
  117. /// <summary>
  118. /// Translate an error to another type
  119. /// </summary>
  120. /// <typeparam name="T2">The original type</typeparam>
  121. /// <returns>The original error with the type T</returns>
  122. public OpResult<T2> To<T2>()
  123. {
  124. return OpResult<T2>.Error(this);
  125. }
  126. /// <summary>
  127. /// Implicit converion to bool
  128. /// </summary>
  129. /// <param name="res">The result to be converted</param>
  130. /// <returns>True if Success, false otherwise</returns>
  131. public static implicit operator bool(OpResult<T> res)
  132. {
  133. return res.Success;
  134. }
  135. protected OpResult()
  136. {
  137. }
  138. }
  139. }