Result.cs 8.5 KB

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