/Lyrics/Lyrics.App/WPH/WPH/Utils/AppErrorList.cs
C# | 340 lines | 151 code | 42 blank | 147 comment | 14 complexity | aa5128e3dc789f1b9245b3d988285ab6 MD5 | raw file
- using System;
- using System.Collections;
- using System.Linq;
-
- namespace WPH.Utils
- {
- /// <summary>
- /// AppErrorList is a collection of <see cref="AppError">AppError</see> objects.
- /// The AppErrorList is useful when you have a batch process that may trigger
- /// a number of exceptions or business rule violations, and you would prefer to
- /// gather the complete list of errors before returning information to the user,
- /// as opposed to stopping at the first error. Input validation is an example
- /// situation when this might be used.
- /// <seealso cref="AppError"/>
- /// <seealso cref="AppErrorFilter"/>
- /// <seealso cref="IErrorProvider"/>
- /// </summary>
- /// <remarks>
- /// For convenience, the <see cref="WPH.Utils.AppErrorList.Add(string, string)">Add</see> method is overloaded to take
- /// the same parameters as the <see cref="AppError">AppError</see> constructors,
- /// so it is not necessary to build the AppError is a separate call.
- /// <para>In imitation of the <see cref="ArrayList">ArrayList</see> class, AppErrorList
- /// has an <see cref="WPH.Utils.AppErrorList.AddRange">AddRange</see> method that will apppend another
- /// AppErrorList to the current instance.</para>
- /// </remarks>
- [Serializable]
- public class AppErrorList : CollectionBase
- {
- /// <summary>
- /// Defines the protected Type _containedType as a "WPH.Utils.AppError" type.
- /// </summary>
- protected Type _containedType = Type.GetType("WPH.Utils.AppError");
-
- #region Extensions
-
- /// <summary>
- /// Creates a new <see cref="AppError">AppError</see> instance and adds it to the collection.
- /// </summary>
- /// <param name="errorId"></param>
- /// <param name="errorMessage"></param>
- /// <returns></returns>
- public int Add(string errorId, string errorMessage)
- {
- return Add( new AppError(errorId, errorMessage) );
- }
-
- /// <summary>
- /// Creates a new <see cref="AppError">AppError</see> instance and adds it to the collection.
- /// </summary>
- /// <param name="errorId"></param>
- /// <param name="errorMessage"></param>
- /// <param name="severity"></param>
- /// <returns></returns>
- public int Add(string errorId, string errorMessage, ErrorSeverity severity)
- {
- return Add( new AppError(errorId, errorMessage, severity) );
- }
-
- /// <summary>
- /// Creates a new <see cref="AppError">AppError</see> instance and adds it to the collection.
- /// </summary>
- /// <param name="ex"></param>
- /// <returns></returns>
- public int Add(Exception ex)
- {
- int retval = Add( new AppError(ex) );
-
- if(ex.InnerException != null)
- { Add(ex.InnerException); // Recursive...
- }
-
- return retval;
- }
-
- /// <summary>
- /// Appends the elements of another AppErrorList to this instance.
- /// </summary>
- /// <param name="list"></param>
- public void AddRange(AppErrorList list)
- {
- InnerList.AddRange(list);
- }
-
- /// <summary>
- /// Returns true if the collection has an error whose severity equals or exceeds
- /// the given ErrorSeverity value.
- /// </summary>
- /// <param name="severity"></param>
- /// <returns></returns>
- public bool ContainsSeverity(ErrorSeverity severity)
- {
- var sev = (int) severity;
-
- return this.Cast<AppError>().Any(err => (int) err.Severity >= sev);
- /*
- foreach( AppError err in this ) {
- if( (int)err.Severity >= sev ) { return true; }
- }
- */
- }
-
- /// <summary>
- /// Returns the combined output of each AppError item's ToString() method,
- /// joined with newline characters.
- /// </summary>
- /// <returns></returns>
- public override string ToString() {
- return ToString("G");
- }
-
- /// <summary>
- /// Overloads the ToString override method which returns the AppError item's ToString() method.
- /// "G" or no parameter at all returns the entire AppError item's set of information.
- /// "M" returns only the error messages.
- /// </summary>
- /// <param name="fmt"></param>
- /// <returns></returns>
- public string ToString(string fmt)
- {
- var buf = new System.Text.StringBuilder(InnerList.Count * 200);
- foreach(AppError err in this)
- {
- switch (fmt.ToUpper()) {
- case "G" :
- buf.Append(err.ToString());
- break;
- case "M" :
- buf.Append(err.Message);
- break;
- default :
- buf.Append(err.ToString());
- break;
- }
- buf.Append("\r\n");
- }
- return buf.ToString();
- }
-
- #endregion
-
- #region CollectionBase method overrides
-
- /// <summary>
- /// Adds the specified value.
- /// </summary>
- /// <param name="value">The value.</param>
- /// <returns></returns>
- public int Add(AppError value)
- {
- return List.Add(value);
- }
-
- /// <summary>
- /// Determines whether [contains] [the specified value].
- /// </summary>
- /// <param name="value">The value.</param>
- /// <returns>
- /// <c>true</c> if [contains] [the specified value]; otherwise, <c>false</c>.
- /// </returns>
- public bool Contains(AppError value)
- {
- return List.Contains(value);
- }
-
- /// <summary>
- /// Indexes the of.
- /// </summary>
- /// <param name="value">The value.</param>
- /// <returns></returns>
- public int IndexOf(AppError value)
- {
- return List.IndexOf(value);
- }
-
- /// <summary>
- /// Inserts the specified index.
- /// </summary>
- /// <param name="index">The index.</param>
- /// <param name="value">The value.</param>
- public void Insert(int index, AppError value)
- {
- List.Insert(index, value);
- }
-
- /// <summary>
- /// Removes the specified value.
- /// </summary>
- /// <param name="value">The value.</param>
- public void Remove(AppError value)
- {
- List.Remove(value);
- }
-
- /// <summary>
- /// Gets or sets the <see cref="WPH.Utils.AppError"/> at the specified index.
- /// </summary>
- /// <value></value>
- public AppError this[int index]
- {
- get { return (AppError)List[index]; }
- set { List[index] = value; }
- }
-
- /// <summary>
- /// Performs additional custom processes before inserting a new element into the <see cref="T:System.Collections.CollectionBase"></see> instance.
- /// </summary>
- /// <param name="index">The zero-based index at which to insert value.</param>
- /// <param name="value">The new value of the element at index.</param>
- protected override void OnInsert(int index, object value)
- {
- base.OnInsert (index, value);
-
- if( (value as AppError) == null )
- throw new ArgumentException("value must be of type AppError.", "value");
- }
-
- /// <summary>
- /// Performs additional custom processes when removing an element from the <see cref="T:System.Collections.CollectionBase"></see> instance.
- /// </summary>
- /// <param name="index">The zero-based index at which value can be found.</param>
- /// <param name="value">The value of the element to remove from index.</param>
- protected override void OnRemove(int index, object value)
- {
- base.OnRemove (index, value);
-
- if( (value as AppError) == null )
- throw new ArgumentException("value must be of type AppError.", "value");
- }
-
- /// <summary>
- /// Performs additional custom processes before setting a value in the <see cref="T:System.Collections.CollectionBase"></see> instance.
- /// </summary>
- /// <param name="index">The zero-based index at which oldValue can be found.</param>
- /// <param name="oldValue">The value to replace with newValue.</param>
- /// <param name="newValue">The new value of the element at index.</param>
- protected override void OnSet(int index, object oldValue, object newValue)
- {
- base.OnSet (index, oldValue, newValue);
-
- if( (newValue as AppError) == null )
- throw new ArgumentException("newValue must be of type AppError.", "newValue");
- }
-
- /// <summary>
- /// Performs additional custom processes when validating a value.
- /// </summary>
- /// <param name="value">The object to validate.</param>
- protected override void OnValidate(object value)
- {
- base.OnValidate (value);
-
- if( (value as AppError) == null )
- throw new ArgumentException("value must be of type AppError.", "value");
- }
-
- #endregion
-
- /// <summary>
- /// Returns an enumerator that iterates through the <see cref="T:System.Collections.CollectionBase"></see> instance.
- /// </summary>
- /// <returns>
- /// An <see cref="T:System.Collections.IEnumerator"></see> for the <see cref="T:System.Collections.CollectionBase"></see> instance.
- /// </returns>
- public new IEnumerator GetEnumerator()
- {
- return new AppErrorListEnumerator(this);
- }
-
-
- #region inner class AppErrorListEnumerator
-
- /// <summary>
- /// Inter-class Enumerator for the AppErrorList object
- /// </summary>
- class AppErrorListEnumerator : IEnumerator
- {
- private int _index = -1;
- private readonly AppErrorList _list;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="AppErrorListEnumerator"/> class.
- /// </summary>
- /// <param name="list">The list.</param>
- public AppErrorListEnumerator(AppErrorList list)
- {
- _list = list;
- }
-
- #region IEnumerator Members
-
- /// <summary>
- /// Sets the enumerator to its initial position, which is before the first element in the collection.
- /// </summary>
- /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
- public void Reset()
- {
- _index = -1;
- }
-
- /// <summary>
- /// Gets the current element in the collection.
- /// </summary>
- /// <value></value>
- /// <returns>The current element in the collection.</returns>
- /// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element. </exception>
- public object Current
- {
- get
- {
- if( _index == -1 )
- throw new InvalidOperationException();
-
- return _list[_index];
- }
- }
-
- /// <summary>
- /// Advances the enumerator to the next element of the collection.
- /// </summary>
- /// <returns>
- /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
- /// </returns>
- /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
- public bool MoveNext()
- {
- _index++;
-
- if (_index < _list.Count) {
- return true;
- }
- Reset();
- return false;
- }
-
- #endregion
- }
-
- #endregion
- }
- }