PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Lyrics/Lyrics.App/WPH/WPH/Utils/AppErrorList.cs

#
C# | 340 lines | 151 code | 42 blank | 147 comment | 14 complexity | aa5128e3dc789f1b9245b3d988285ab6 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. namespace WPH.Utils
  5. {
  6. /// <summary>
  7. /// AppErrorList is a collection of <see cref="AppError">AppError</see> objects.
  8. /// The AppErrorList is useful when you have a batch process that may trigger
  9. /// a number of exceptions or business rule violations, and you would prefer to
  10. /// gather the complete list of errors before returning information to the user,
  11. /// as opposed to stopping at the first error. Input validation is an example
  12. /// situation when this might be used.
  13. /// <seealso cref="AppError"/>
  14. /// <seealso cref="AppErrorFilter"/>
  15. /// <seealso cref="IErrorProvider"/>
  16. /// </summary>
  17. /// <remarks>
  18. /// For convenience, the <see cref="WPH.Utils.AppErrorList.Add(string, string)">Add</see> method is overloaded to take
  19. /// the same parameters as the <see cref="AppError">AppError</see> constructors,
  20. /// so it is not necessary to build the AppError is a separate call.
  21. /// <para>In imitation of the <see cref="ArrayList">ArrayList</see> class, AppErrorList
  22. /// has an <see cref="WPH.Utils.AppErrorList.AddRange">AddRange</see> method that will apppend another
  23. /// AppErrorList to the current instance.</para>
  24. /// </remarks>
  25. [Serializable]
  26. public class AppErrorList : CollectionBase
  27. {
  28. /// <summary>
  29. /// Defines the protected Type _containedType as a "WPH.Utils.AppError" type.
  30. /// </summary>
  31. protected Type _containedType = Type.GetType("WPH.Utils.AppError");
  32. #region Extensions
  33. /// <summary>
  34. /// Creates a new <see cref="AppError">AppError</see> instance and adds it to the collection.
  35. /// </summary>
  36. /// <param name="errorId"></param>
  37. /// <param name="errorMessage"></param>
  38. /// <returns></returns>
  39. public int Add(string errorId, string errorMessage)
  40. {
  41. return Add( new AppError(errorId, errorMessage) );
  42. }
  43. /// <summary>
  44. /// Creates a new <see cref="AppError">AppError</see> instance and adds it to the collection.
  45. /// </summary>
  46. /// <param name="errorId"></param>
  47. /// <param name="errorMessage"></param>
  48. /// <param name="severity"></param>
  49. /// <returns></returns>
  50. public int Add(string errorId, string errorMessage, ErrorSeverity severity)
  51. {
  52. return Add( new AppError(errorId, errorMessage, severity) );
  53. }
  54. /// <summary>
  55. /// Creates a new <see cref="AppError">AppError</see> instance and adds it to the collection.
  56. /// </summary>
  57. /// <param name="ex"></param>
  58. /// <returns></returns>
  59. public int Add(Exception ex)
  60. {
  61. int retval = Add( new AppError(ex) );
  62. if(ex.InnerException != null)
  63. { Add(ex.InnerException); // Recursive...
  64. }
  65. return retval;
  66. }
  67. /// <summary>
  68. /// Appends the elements of another AppErrorList to this instance.
  69. /// </summary>
  70. /// <param name="list"></param>
  71. public void AddRange(AppErrorList list)
  72. {
  73. InnerList.AddRange(list);
  74. }
  75. /// <summary>
  76. /// Returns true if the collection has an error whose severity equals or exceeds
  77. /// the given ErrorSeverity value.
  78. /// </summary>
  79. /// <param name="severity"></param>
  80. /// <returns></returns>
  81. public bool ContainsSeverity(ErrorSeverity severity)
  82. {
  83. var sev = (int) severity;
  84. return this.Cast<AppError>().Any(err => (int) err.Severity >= sev);
  85. /*
  86. foreach( AppError err in this ) {
  87. if( (int)err.Severity >= sev ) { return true; }
  88. }
  89. */
  90. }
  91. /// <summary>
  92. /// Returns the combined output of each AppError item's ToString() method,
  93. /// joined with newline characters.
  94. /// </summary>
  95. /// <returns></returns>
  96. public override string ToString() {
  97. return ToString("G");
  98. }
  99. /// <summary>
  100. /// Overloads the ToString override method which returns the AppError item's ToString() method.
  101. /// "G" or no parameter at all returns the entire AppError item's set of information.
  102. /// "M" returns only the error messages.
  103. /// </summary>
  104. /// <param name="fmt"></param>
  105. /// <returns></returns>
  106. public string ToString(string fmt)
  107. {
  108. var buf = new System.Text.StringBuilder(InnerList.Count * 200);
  109. foreach(AppError err in this)
  110. {
  111. switch (fmt.ToUpper()) {
  112. case "G" :
  113. buf.Append(err.ToString());
  114. break;
  115. case "M" :
  116. buf.Append(err.Message);
  117. break;
  118. default :
  119. buf.Append(err.ToString());
  120. break;
  121. }
  122. buf.Append("\r\n");
  123. }
  124. return buf.ToString();
  125. }
  126. #endregion
  127. #region CollectionBase method overrides
  128. /// <summary>
  129. /// Adds the specified value.
  130. /// </summary>
  131. /// <param name="value">The value.</param>
  132. /// <returns></returns>
  133. public int Add(AppError value)
  134. {
  135. return List.Add(value);
  136. }
  137. /// <summary>
  138. /// Determines whether [contains] [the specified value].
  139. /// </summary>
  140. /// <param name="value">The value.</param>
  141. /// <returns>
  142. /// <c>true</c> if [contains] [the specified value]; otherwise, <c>false</c>.
  143. /// </returns>
  144. public bool Contains(AppError value)
  145. {
  146. return List.Contains(value);
  147. }
  148. /// <summary>
  149. /// Indexes the of.
  150. /// </summary>
  151. /// <param name="value">The value.</param>
  152. /// <returns></returns>
  153. public int IndexOf(AppError value)
  154. {
  155. return List.IndexOf(value);
  156. }
  157. /// <summary>
  158. /// Inserts the specified index.
  159. /// </summary>
  160. /// <param name="index">The index.</param>
  161. /// <param name="value">The value.</param>
  162. public void Insert(int index, AppError value)
  163. {
  164. List.Insert(index, value);
  165. }
  166. /// <summary>
  167. /// Removes the specified value.
  168. /// </summary>
  169. /// <param name="value">The value.</param>
  170. public void Remove(AppError value)
  171. {
  172. List.Remove(value);
  173. }
  174. /// <summary>
  175. /// Gets or sets the <see cref="WPH.Utils.AppError"/> at the specified index.
  176. /// </summary>
  177. /// <value></value>
  178. public AppError this[int index]
  179. {
  180. get { return (AppError)List[index]; }
  181. set { List[index] = value; }
  182. }
  183. /// <summary>
  184. /// Performs additional custom processes before inserting a new element into the <see cref="T:System.Collections.CollectionBase"></see> instance.
  185. /// </summary>
  186. /// <param name="index">The zero-based index at which to insert value.</param>
  187. /// <param name="value">The new value of the element at index.</param>
  188. protected override void OnInsert(int index, object value)
  189. {
  190. base.OnInsert (index, value);
  191. if( (value as AppError) == null )
  192. throw new ArgumentException("value must be of type AppError.", "value");
  193. }
  194. /// <summary>
  195. /// Performs additional custom processes when removing an element from the <see cref="T:System.Collections.CollectionBase"></see> instance.
  196. /// </summary>
  197. /// <param name="index">The zero-based index at which value can be found.</param>
  198. /// <param name="value">The value of the element to remove from index.</param>
  199. protected override void OnRemove(int index, object value)
  200. {
  201. base.OnRemove (index, value);
  202. if( (value as AppError) == null )
  203. throw new ArgumentException("value must be of type AppError.", "value");
  204. }
  205. /// <summary>
  206. /// Performs additional custom processes before setting a value in the <see cref="T:System.Collections.CollectionBase"></see> instance.
  207. /// </summary>
  208. /// <param name="index">The zero-based index at which oldValue can be found.</param>
  209. /// <param name="oldValue">The value to replace with newValue.</param>
  210. /// <param name="newValue">The new value of the element at index.</param>
  211. protected override void OnSet(int index, object oldValue, object newValue)
  212. {
  213. base.OnSet (index, oldValue, newValue);
  214. if( (newValue as AppError) == null )
  215. throw new ArgumentException("newValue must be of type AppError.", "newValue");
  216. }
  217. /// <summary>
  218. /// Performs additional custom processes when validating a value.
  219. /// </summary>
  220. /// <param name="value">The object to validate.</param>
  221. protected override void OnValidate(object value)
  222. {
  223. base.OnValidate (value);
  224. if( (value as AppError) == null )
  225. throw new ArgumentException("value must be of type AppError.", "value");
  226. }
  227. #endregion
  228. /// <summary>
  229. /// Returns an enumerator that iterates through the <see cref="T:System.Collections.CollectionBase"></see> instance.
  230. /// </summary>
  231. /// <returns>
  232. /// An <see cref="T:System.Collections.IEnumerator"></see> for the <see cref="T:System.Collections.CollectionBase"></see> instance.
  233. /// </returns>
  234. public new IEnumerator GetEnumerator()
  235. {
  236. return new AppErrorListEnumerator(this);
  237. }
  238. #region inner class AppErrorListEnumerator
  239. /// <summary>
  240. /// Inter-class Enumerator for the AppErrorList object
  241. /// </summary>
  242. class AppErrorListEnumerator : IEnumerator
  243. {
  244. private int _index = -1;
  245. private readonly AppErrorList _list;
  246. /// <summary>
  247. /// Initializes a new instance of the <see cref="AppErrorListEnumerator"/> class.
  248. /// </summary>
  249. /// <param name="list">The list.</param>
  250. public AppErrorListEnumerator(AppErrorList list)
  251. {
  252. _list = list;
  253. }
  254. #region IEnumerator Members
  255. /// <summary>
  256. /// Sets the enumerator to its initial position, which is before the first element in the collection.
  257. /// </summary>
  258. /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
  259. public void Reset()
  260. {
  261. _index = -1;
  262. }
  263. /// <summary>
  264. /// Gets the current element in the collection.
  265. /// </summary>
  266. /// <value></value>
  267. /// <returns>The current element in the collection.</returns>
  268. /// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element. </exception>
  269. public object Current
  270. {
  271. get
  272. {
  273. if( _index == -1 )
  274. throw new InvalidOperationException();
  275. return _list[_index];
  276. }
  277. }
  278. /// <summary>
  279. /// Advances the enumerator to the next element of the collection.
  280. /// </summary>
  281. /// <returns>
  282. /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
  283. /// </returns>
  284. /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
  285. public bool MoveNext()
  286. {
  287. _index++;
  288. if (_index < _list.Count) {
  289. return true;
  290. }
  291. Reset();
  292. return false;
  293. }
  294. #endregion
  295. }
  296. #endregion
  297. }
  298. }