You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.8 KiB

  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer
  6. {
  7. /// <summary>结果状态,Code 为零时表示正常,Code 0 与 NULL 相等。</summary>
  8. [Serializable]
  9. public class Result
  10. {
  11. private int _code;
  12. private string _message;
  13. private Exception _exception;
  14. /// <summary>代码。</summary>
  15. public int Code { get { return _code; } }
  16. /// <summary>消息。</summary>
  17. public string Message { get { return _message; } }
  18. /// <summary>异常。</summary>
  19. public Exception Exception { get { return _exception; } }
  20. /// <summary>创建实例:Code = 0,Message = NULL,Exception = NULL。</summary>
  21. public Result() { Construct(0, null, null); }
  22. /// <summary>创建实例:Message = NULL,Exception = NULL。</summary>
  23. public Result(int code) { Construct(code, null, null); }
  24. /// <summary>创建实例:Exception = NULL。</summary>
  25. public Result(int code, string message) { Construct(code, message, null); }
  26. /// <summary>创建实例:Code = -1,Exception = NULL。</summary>
  27. public Result(string message) { Construct(-1, message, null); }
  28. /// <summary>创建实例:Code = -2。</summary>
  29. public Result(Exception exception) { Construct(-2, null, exception); }
  30. private void Construct(int code, string message, Exception exception)
  31. {
  32. _code = code;
  33. if (exception == null)
  34. {
  35. _message = message;
  36. }
  37. else
  38. {
  39. _exception = exception;
  40. try { _message = _exception.Message; } catch { }
  41. }
  42. }
  43. /// <summary>判断相等。</summary>
  44. public override bool Equals(object obj)
  45. {
  46. return Equals(this, obj as Result);
  47. }
  48. /// <summary>获取哈希码。</summary>
  49. public override int GetHashCode()
  50. {
  51. return _code;
  52. }
  53. /// <summary>返回格式化的字符串,格式为:<para>Code 0 : Message Content</para></summary>
  54. public override string ToString()
  55. {
  56. var sb = new StringBuilder();
  57. sb.Append("Code ");
  58. sb.Append(_code.ToString());
  59. sb.Append(" : ");
  60. if (!string.IsNullOrEmpty(_message)) sb.Append(_message);
  61. var text = sb.ToString();
  62. return text;
  63. }
  64. /// <summary>判断相等。</summary>
  65. public static bool operator ==(Result left, Result right)
  66. {
  67. return Equals(left, right);
  68. }
  69. /// <summary>判断不等。</summary>
  70. public static bool operator !=(Result left, Result right)
  71. {
  72. return !Equals(left, right);
  73. }
  74. /// <summary>与 System.Int32 转换 Code 属性。</summary>
  75. public static implicit operator int(Result result)
  76. {
  77. return result == null ? 0 : result._code;
  78. }
  79. /// <summary>与 System.Int32 转换 Code 属性。</summary>
  80. public static implicit operator Result(int code)
  81. {
  82. return new Result(code);
  83. }
  84. private static bool Equals(Result left, Result right)
  85. {
  86. if (left as object == null)
  87. {
  88. if (right as object == null)
  89. {
  90. return true;
  91. }
  92. else
  93. {
  94. return right._code == 0;
  95. }
  96. }
  97. else
  98. {
  99. if (right as object == null)
  100. {
  101. return left._code == 0;
  102. }
  103. else
  104. {
  105. return left._code == right._code;
  106. }
  107. }
  108. }
  109. /// <summary>Code = 0,Message = NULL,Exception = NULL。</summary>
  110. public static Result Zero { get; } = new Result();
  111. }
  112. /// <summary>装箱返回结果,T 不适用于 System.String。</summary>
  113. [Serializable]
  114. public class Result<T> : IDisposable
  115. {
  116. private static readonly T Default = default(T);
  117. private T _entity = default(T);
  118. private Exception _exception = null;
  119. private int _code = 0;
  120. private string _message = Constant.EmptyString;
  121. /// <summary>对象。</summary>
  122. public T Entity { get { return _entity; } }
  123. /// <summary>异常。</summary>
  124. public Exception Exception { get { return _exception; } }
  125. /// <summary>代码。</summary>
  126. public int Code { get { return _code; } }
  127. /// <summary>消息,此属性永不为 Null 值,当存在 Exception 时将被 Exception.Message 强制覆盖。</summary>
  128. public string Message { get { return _message; } }
  129. /// <summary>含有实体对象。</summary>
  130. public bool HasEntity { get { return _entity != null; } }
  131. /// <summary>执行与释放或重置非托管资源关联的应用程序定义的任务。</summary>
  132. public void Dispose()
  133. {
  134. var entity = _entity as IDisposable;
  135. if (entity != null) entity.Dispose();
  136. }
  137. private void Construct(T entity, Exception exception, string message)
  138. {
  139. _entity = entity;
  140. _exception = exception;
  141. _message = message ?? Constant.EmptyString;
  142. if (_exception != null && TextVerifier.IsBlank(_message))
  143. {
  144. _message = _exception.Message;
  145. }
  146. }
  147. /// <summary>创建空结果,不含有实体、消息和异常。</summary>
  148. public Result() { }
  149. /// <summary>创建带有异常的结果。</summary>
  150. public Result(Exception exception) { Construct(Default, exception, null); }
  151. /// <summary>创建带有实体的结果。</summary>
  152. public Result(T entity) { Construct(entity, null, null); }
  153. /// <summary>创建带有实体和消息的结果。</summary>
  154. public Result(T entity, params string[] message) { Construct(entity, null, TextGenerator.Merge(message)); }
  155. /// <summary>创建带有错误消息的结果。</summary>
  156. public static Result<T> Error(params string[] message)
  157. {
  158. var instance = new Result<T>();
  159. var merged = TextGenerator.Merge(message);
  160. instance.Construct(Default, null, merged.IsEmpty() ? "存在无效消息。" : merged);
  161. return instance;
  162. }
  163. /// <summary>创建带有异常的结果,Exception 会覆盖 Message。</summary>
  164. public static Result<T> Error(Exception exception)
  165. {
  166. var instance = new Result<T>();
  167. instance.Construct(Default, exception, (exception == null ? "存在 NULL 异常。" : null));
  168. return instance;
  169. }
  170. }
  171. }