Result.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System.Net;
  2. using System.Runtime.Serialization;
  3. using EasyDevCore.Common;
  4. namespace EasyDevCore.Remote
  5. {
  6. /// <summary>
  7. /// Enum ResultMode
  8. /// </summary>
  9. public enum ResultMode
  10. {
  11. /// <summary>
  12. /// The success
  13. /// </summary>
  14. Success = 0,
  15. /// <summary>
  16. /// The error
  17. /// </summary>
  18. Error = 1,
  19. /// <summary>
  20. /// The information
  21. /// </summary>
  22. Info = 2,
  23. /// <summary>
  24. /// The warning
  25. /// </summary>
  26. Warning = 3,
  27. /// <summary>
  28. /// The Confirmation
  29. /// </summary>
  30. Confirmation = 4
  31. }
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. /// <typeparam name="TData">The type of the data.</typeparam>
  36. public interface IValueResult<TData>
  37. {
  38. /// <summary>
  39. /// Gets or sets the data.
  40. /// </summary>
  41. /// <value>
  42. /// The data.
  43. /// </value>
  44. TData Data { get; set; }
  45. /// <summary>
  46. /// Gets a value indicating whether this instance is error.
  47. /// </summary>
  48. /// <value>
  49. /// <c>true</c> if this instance is error; otherwise, <c>false</c>.
  50. /// </value>
  51. bool IsError { get; }
  52. /// <summary>
  53. /// Gets a value indicating whether this instance is success.
  54. /// </summary>
  55. /// <value>
  56. /// <c>true</c> if this instance is success; otherwise, <c>false</c>.
  57. /// </value>
  58. bool IsSuccess { get; }
  59. /// <summary>
  60. /// Gets or sets the message.
  61. /// </summary>
  62. /// <value>
  63. /// The message.
  64. /// </value>
  65. string Message { get; set; }
  66. /// <summary>
  67. /// Gets or sets the result code.
  68. /// </summary>
  69. /// <value>
  70. /// The result code.
  71. /// </value>
  72. string ResultCode { get; set; }
  73. /// <summary>
  74. /// Gets or sets the result status.
  75. /// </summary>
  76. /// <value>
  77. /// The result status.
  78. /// </value>
  79. ResultMode ResultStatus { get; set; }
  80. }
  81. /// <summary>
  82. ///
  83. /// </summary>
  84. /// <typeparam name="TData">The type of the data.</typeparam>
  85. /// <seealso cref="IValueResult&lt;TData&gt;" />
  86. public abstract class ValueResultBase<TData> : IValueResult<TData>
  87. {
  88. /// <summary>
  89. /// Gets or sets the data.
  90. /// </summary>
  91. /// <value>
  92. /// The data.
  93. /// </value>
  94. public TData Data { get; set; }
  95. /// <summary>
  96. /// Gets or sets the result status.
  97. /// </summary>
  98. /// <value>
  99. /// The result status.
  100. /// </value>
  101. public ResultMode ResultStatus { get; set; } = ResultMode.Success;
  102. /// <summary>
  103. /// Gets or sets the result code.
  104. /// </summary>
  105. /// <value>
  106. /// The result code.
  107. /// </value>
  108. public string ResultCode { get; set; } = "";
  109. /// <summary>
  110. /// Gets or sets the message.
  111. /// </summary>
  112. /// <value>
  113. /// The message.
  114. /// </value>
  115. public string Message { get; set; } = "";
  116. /// <summary>
  117. /// Gets a value indicating whether this instance is error.
  118. /// </summary>
  119. /// <value>
  120. /// <c>true</c> if this instance is error; otherwise, <c>false</c>.
  121. /// </value>
  122. public virtual bool IsError => ResultStatus == ResultMode.Error;
  123. /// <summary>
  124. /// Gets a value indicating whether this instance is success.
  125. /// </summary>
  126. /// <value>
  127. /// <c>true</c> if this instance is success; otherwise, <c>false</c>.
  128. /// </value>
  129. public bool IsSuccess => !IsError;
  130. /// <summary>
  131. /// Maps the specified convert.
  132. /// </summary>
  133. /// <typeparam name="TMapData">The type of the map data.</typeparam>
  134. /// <param name="convert">The convert.</param>
  135. /// <returns></returns>
  136. public abstract ValueResultBase<TMapData> Map<TMapData>(Func<TData, TMapData> convert);
  137. }
  138. /// <summary>
  139. ///
  140. /// </summary>
  141. /// <typeparam name="TData">The type of the value.</typeparam>
  142. public class Result<TData> : ValueResultBase<TData>
  143. {
  144. /// <summary>
  145. /// Gets or sets the error.
  146. /// </summary>
  147. /// <value>
  148. /// The error.
  149. /// </value>
  150. [IgnoreDataMember]
  151. public Exception Error { get; set; }
  152. /// <summary>
  153. /// Gets a value indicating whether this instance is error.
  154. /// </summary>
  155. /// <value>
  156. /// <c>true</c> if this instance is error; otherwise, <c>false</c>.
  157. /// </value>
  158. public override bool IsError => ResultStatus == ResultMode.Error || Error != null;
  159. /// <summary>
  160. /// Gets or sets the HTTP result status.
  161. /// </summary>
  162. /// <value>
  163. /// The HTTP result status.
  164. /// </value>
  165. [IgnoreDataMember]
  166. public HttpStatusCode HttpResultStatus { get; set; } = HttpStatusCode.OK;
  167. /// <summary>
  168. /// Initializes a new instance of the <see cref="Result{TData}"/> class.
  169. /// </summary>
  170. internal Result()
  171. {
  172. }
  173. /// <summary>
  174. /// Initializes a new instance of the <see cref="Result{TData}"/> class.
  175. /// </summary>
  176. /// <param name="data">The data.</param>
  177. /// <param name="resultStatus">The result status.</param>
  178. /// <param name="resultCode">The result code.</param>
  179. /// <param name="message">The message.</param>
  180. public Result(TData data, ResultMode resultStatus = ResultMode.Success, string resultCode = "", string message = "")
  181. {
  182. Data = data;
  183. ResultStatus = resultStatus;
  184. ResultCode = resultCode;
  185. Message = message;
  186. }
  187. /// <summary>
  188. /// Initializes a new instance of the <see cref="Result{TData}" /> class.
  189. /// </summary>
  190. /// <param name="data">The data.</param>
  191. /// <param name="error">The exception.</param>
  192. public Result(TData data, Exception error)
  193. {
  194. ResultCode = error.HResult.ToString();
  195. ResultStatus = ResultMode.Error;
  196. Message = error.Message;
  197. HttpResultStatus = HttpStatusCode.BadRequest;
  198. Error = error;
  199. }
  200. /// <summary>
  201. /// Maps the specified convert.
  202. /// </summary>
  203. /// <typeparam name="TMapData">The type of the map data.</typeparam>
  204. /// <param name="convert">The convert.</param>
  205. /// <returns></returns>
  206. public override Result<TMapData> Map<TMapData>(Func<TData, TMapData> convert)
  207. {
  208. return new Result<TMapData>
  209. {
  210. Data = convert(Data!),
  211. Message = Message,
  212. ResultCode = ResultCode,
  213. ResultStatus = ResultStatus,
  214. Error = Error,
  215. HttpResultStatus = HttpResultStatus
  216. };
  217. }
  218. }
  219. /// <summary>
  220. ///
  221. /// </summary>
  222. /// <typeparam name="TData">The type of the value.</typeparam>
  223. /// <seealso cref="Result&lt;TValue&gt;" />
  224. public class UpdateResult<TData> : Result<TData>
  225. {
  226. /// <summary>
  227. /// Gets or sets the updated date.
  228. /// </summary>
  229. /// <value>
  230. /// The updated date.
  231. /// </value>
  232. public object UpdatedDate { get; set; }
  233. /// <summary>
  234. /// Gets or sets the updated by.
  235. /// </summary>
  236. /// <value>
  237. /// The updated by.
  238. /// </value>
  239. public object UpdatedBy { get; set; }
  240. /// <summary>
  241. /// Initializes a new instance of the <see cref="UpdateResult{TData}"/> class.
  242. /// </summary>
  243. internal UpdateResult()
  244. {
  245. }
  246. /// <summary>
  247. /// Initializes a new instance of the <see cref="UpdateResult{TValue}"/> class.
  248. /// </summary>
  249. /// <param name="data">The data.</param>
  250. /// <param name="updatedDate">The updated date.</param>
  251. /// <param name="updatedBy">The updated by.</param>
  252. /// <param name="resultStatus">The result status.</param>
  253. /// <param name="resultCode">The result code.</param>
  254. /// <param name="message">The message.</param>
  255. public UpdateResult(TData data, object updatedDate, object updatedBy, ResultMode resultStatus = ResultMode.Success, string resultCode = "", string message = "") : base(data, resultStatus, resultCode, message)
  256. {
  257. UpdatedDate = updatedDate;
  258. UpdatedBy = updatedBy;
  259. }
  260. /// <summary>
  261. /// Initializes a new instance of the <see cref="Result{TValue}"/> class.
  262. /// </summary>
  263. /// <param name="data">The data.</param>
  264. /// <param name="error">The exception.</param>
  265. public UpdateResult(TData data, Exception error) : base(data, error)
  266. {
  267. }
  268. /// <summary>
  269. /// Maps the specified convert.
  270. /// </summary>
  271. /// <typeparam name="TMapData">The type of the map data.</typeparam>
  272. /// <param name="convert">The convert.</param>
  273. /// <returns></returns>
  274. public override UpdateResult<TMapData> Map<TMapData>(Func<TData, TMapData> convert)
  275. {
  276. return new UpdateResult<TMapData>
  277. {
  278. Data = convert(Data!),
  279. Message = Message,
  280. ResultCode = ResultCode,
  281. ResultStatus = ResultStatus,
  282. UpdatedBy = UpdatedBy,
  283. UpdatedDate = UpdatedDate,
  284. Error = Error,
  285. HttpResultStatus = HttpResultStatus
  286. };
  287. }
  288. }
  289. }