Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET

Developing Next Generation Smart Clients using .NET 2.0 working with Existing .NET 1.1 SOA-based XML Web Services

Rate me:
Please Sign up or sign in to vote.
4.96/5 (134 votes)
16 Aug 200540 min read 1.2M   3.9K   462  
Comprehensive guide to development of .NET 2.0 Smart Clients working with existing Service Oriented Architecture based XML web services, fully utilizing the Enterprise Library
	
// =====================================================================================
// Copyright © 2005 by . All rights are reserved.
// 
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
// 
// website , email OmarALZabir@gmail.com, msn oazabir@hotmail.com
// =====================================================================================
using System.ComponentModel;
using System.Collections;
using System;
using System.Diagnostics;
using System.Xml.Serialization;

namespace SmartInstitute
{
	///<summary>
	/// This class is a strong typed collection of <see cref="Account"/> objects that support sorting and binding.
	/// <remarks>
	/// It implements the IClonable, IBindingList and IList interfaces.
	/// </remarks>
	///</summary>
	[Serializable]
	public class AccountCollectionBase : CollectionBase , IBindingList, IList, ICloneable
	{
		private ArrayList _SortedList = new ArrayList();
		private ArrayList _OriginalList;
		private bool _issorted = false;
		private PropertyDescriptor _sortby;
		private ListSortDirection _sortdirection = ListSortDirection.Descending;
	
		#region "ListItem"

		private class ListItem : IComparable 
		{
			/// <summary>
			/// The Key of the List Item.
			/// </summary>
			public object Key;
			
			
			/// <summary>
			/// The Item associated with the key.
			/// </summary>
			public object Item;
	
	
			/// <summary>
			/// Creates a new <see cref="ListItem"/> instance.
			/// </summary>
			/// <param name="key">Key.</param>
			/// <param name="item">Item.</param>
			public ListItem(object key, object item)
			{
				Key = key;
				Item = item;
			}
	
	
			///<summary>
			/// Compares the current instance with another object of the same type.
			///</summary>
			///<param name="obj">An object to compare with this instance.</param>
			///<returns>
			/// A 32-bit signed integer that indicates the relative order of the comparands. The return value has these meanings:
			/// <list type="table">
			///    <listheader>
			/// 	  <term>Value</term>
			/// 	  <description>Meaning</description>
			///    </listheader>
			///    <item>
			/// 	  <term>Less than zero</term>
			/// 	  <description>This instance is less than obj.</description>
			///    </item>
			///    <item>
			/// 	  <term>Zero</term>
			/// 	  <description>This instance is equal to obj.</description>
			///    </item>
			///    <item>
			/// 	  <term>Greater than zero</term>
			/// 	  <description>This instance is greater than obj.</description>
			///    </item>
			/// </list>
			///</returns>
			int IComparable.CompareTo(object obj)
			{
				object target = ((ListItem)obj).Key;
	
				if(Key is IComparable)
				{
					return ((IComparable)Key).CompareTo(target);
				}
				else
				{
					//Debug.WriteLine("No Comparable");
					if(Key.Equals(target))
						return 0;
					else
						return Key.ToString().CompareTo(target.ToString());
				}
			}
		
	
			///<summary>
			/// Obtains the <see cref="System.String"/> representation of this instance.
			///</summary>
			///<returns>The key of the item.</returns>
			public override string ToString()
			{
				return Key.ToString ();
			}
		}
		#endregion
	
		#region "Find"

		///<summary>
		/// Finds the first <see cref="Account" /> object in the current list matching the search criteria.
		///</summary>
		/// <param name="searchfield">Field of the object to search.</param>
		/// <param name="searchvalue">Value to find.</param>
		public Account Find(AccountColumns searchfield, object searchvalue)
		{
			PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(Account));
			PropertyDescriptor searchby = null;
			switch(searchfield)
			{
				case AccountColumns.ID:
					searchby = props["ID"];
					break;
				case AccountColumns.StudentID:
					searchby = props["StudentID"];
					break;
				case AccountColumns.TransactionDescription:
					searchby = props["TransactionDescription"];
					break;
				case AccountColumns.TransactionDate:
					searchby = props["TransactionDate"];
					break;
				case AccountColumns.DebitAmount:
					searchby = props["DebitAmount"];
					break;
				case AccountColumns.CreditAmount:
					searchby = props["CreditAmount"];
					break;
				case AccountColumns.Balance:
					searchby = props["Balance"];
					break;
				case AccountColumns.ChangeStamp:
					searchby = props["ChangeStamp"];
					break;
				case AccountColumns.Type:
					searchby = props["Type"];
					break;
				case AccountColumns.BankName:
					searchby = props["BankName"];
					break;
				case AccountColumns.CheckNumber:
					searchby = props["CheckNumber"];
					break;
				case AccountColumns.ORNumber:
					searchby = props["ORNumber"];
					break;
			}
			int j = ((IBindingList)this).Find(searchby, searchvalue);
			if (j > -1)
				return this[j];
			else
				return null;
		}
		
		///<summary>
		/// Finds a collection of <see cref="Account" /> objects in the current list.
		///</summary>
		/// <param name="searchfield">Field of the object to search.</param>
		/// <param name="searchvalue">Value to find.</param>
		public AccountCollection FindAll(AccountColumns searchfield, object searchvalue)
		{
			PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(Account));
			PropertyDescriptor searchby = null;
			switch(searchfield)
			{
				case AccountColumns.ID:
					searchby = props["ID"];
					break;
				case AccountColumns.StudentID:
					searchby = props["StudentID"];
					break;
				case AccountColumns.TransactionDescription:
					searchby = props["TransactionDescription"];
					break;
				case AccountColumns.TransactionDate:
					searchby = props["TransactionDate"];
					break;
				case AccountColumns.DebitAmount:
					searchby = props["DebitAmount"];
					break;
				case AccountColumns.CreditAmount:
					searchby = props["CreditAmount"];
					break;
				case AccountColumns.Balance:
					searchby = props["Balance"];
					break;
				case AccountColumns.ChangeStamp:
					searchby = props["ChangeStamp"];
					break;
				case AccountColumns.Type:
					searchby = props["Type"];
					break;
				case AccountColumns.BankName:
					searchby = props["BankName"];
					break;
				case AccountColumns.CheckNumber:
					searchby = props["CheckNumber"];
					break;
				case AccountColumns.ORNumber:
					searchby = props["ORNumber"];
					break;
			}
	
			AccountCollection copy = new AccountCollection();
			foreach(Account _Account in this.List)
			{
				if (searchby.GetValue(_Account).Equals(searchvalue))
				{
					Account copyAccount = (Account)MakeCopyOf(_Account);
					copy.Add(copyAccount);	
				}
			}
			return copy;
		}

		#endregion
	
		#region "Sort Code"
		
		///<summary>
		/// Sorts the elements in the AccountCollectionBase .
		///</summary>
		private void SortList()
		{
			_SortedList.Clear();
			//Load List to sort.
			if (_sortby == null)
			{
				foreach(object obj in this.InnerList)
				{
					_SortedList.Add(new ListItem(obj,obj));
				}
			}
			else
			{
				foreach(object obj in this.InnerList)
				{
					_SortedList.Add(new ListItem(_sortby.GetValue(obj), obj));
				}
			}
			//if not already sorted, create a backup of original
			if(!_issorted)
				_OriginalList = new ArrayList(List);
			//Sort List
			_SortedList.Sort();
			//Clear real list.
			InnerList.Clear();
			//re-add item in sorted order.
			if(_sortdirection == ListSortDirection.Ascending)
			{
				for(int x=0; x < _SortedList.Count;x++)
				{
					base.InnerList.Add(((ListItem)_SortedList[x]).Item );
				}
			}
			else
			{
				for(int x=_SortedList.Count-1; x != -1;x--)
				{
					base.InnerList.Add(((ListItem)_SortedList[x]).Item );
				}
			}
			_issorted = true;

			OnListChanged(new ListChangedEventArgs(ListChangedType.Reset,0));
		}
		
					
		/// <summary>
		///		Sorts the collection based upon field selected.
		/// </summary>
		/// <param name="field">Field of the object on which to sort.</param>
		/// <param name="direction">Direction to sort in, Ascending or Descending.</param>
		public void Sort(AccountColumns field, ListSortDirection direction)
		{
			this._sortby = TypeDescriptor.GetProperties(typeof(Account)).Find( field.ToString(), false );
			this._sortdirection = direction;
			SortList();
		}	
		
		
		/// <summary>
		///		Sorts the collection based on primary key.
		/// </summary>
		/// <param name="direction">Direction to sort in, Ascending or Descending.</param>
		public void  Sort(ListSortDirection direction)
		{
			_sortby = null;
			_sortdirection = direction;
			SortList();
		}
		
		
		/// <summary>
		///		Sorts the collection based on primary key. Sorts in Ascending order.
		/// </summary>
		public void Sort()
		{
			_sortby = null;
			_sortdirection = ListSortDirection.Ascending;
			SortList();
		}
		
		#endregion
		
		#region Shuffle
		
		/// <summary>
		///		Sorts the collection based on a random shuffle.
		/// </summary>
		/// <author>Steven Smith</author>
		/// <url>http://blogs.aspadvice.com/ssmith/archive/2005/01/27/2480.aspx</url>
		public void Shuffle()
		{
		  ArrayList source = this.InnerList;
		  Random rnd = new Random();
		  for (int inx = source.Count-1; inx > 0; inx--)
		  {
		    int position = rnd.Next(inx+1);
		    object temp = source[inx];
		    source[inx] = source[position];
		    source[position] = temp;
		  }
		}
		#endregion
				
		#region Typed Collection Methods
		
		/// <summary>
		///	Adds a new Account instance to the Collection.
		/// </summary>
		/// <param name="value"><see cref="Product"/> instance.</param>
		/// <returns></returns>
		public int Add (Account value) 
		{
			return List.Add(value);
		}
		
		/// <summary>
		///	Adds a new Account instance to the Collection.
		/// </summary>
		///<param name="ID"></param>
		///<param name="StudentID"></param>
		///<param name="TransactionDescription"></param>
		///<param name="TransactionDate"></param>
		///<param name="DebitAmount"></param>
		///<param name="CreditAmount"></param>
		///<param name="Balance"></param>
		///<param name="ChangeStamp"></param>
		///<param name="Type"></param>
		///<param name="BankName"></param>
		///<param name="CheckNumber"></param>
		///<param name="ORNumber"></param>
		/// <returns></returns>
		public int Add (System.Int32 ID, System.Int32 StudentID, System.String TransactionDescription, System.DateTime TransactionDate, System.Decimal DebitAmount, System.Decimal CreditAmount, System.Decimal Balance, System.DateTime ChangeStamp, System.Int32 Type, System.String BankName, System.String CheckNumber, System.String ORNumber) 
		{
			return List.Add(new Account(ID, StudentID, TransactionDescription, TransactionDate, DebitAmount, CreditAmount, Balance, ChangeStamp, Type, BankName, CheckNumber, ORNumber));
		}
		/// <summary>
		///	Adds a new Account to the Collection.
		/// </summary>
		/// <returns>Product object.</returns>
		public Account AddNew() 
		{
			return (Account)((IBindingList)this).AddNew();
		}
		
		
		/// <summary>
		/// Gets or sets the <see cref="Account"/> at the specified index.
		/// </summary>
		/// <value></value>
		public Account this[int index] 
		{
			get 
			{
				return (Account)(List[index]);
			}
			set 
			{
				List[index] = value;
			}
		}
		
		
		/// <summary>
		///	Removes a Account object from the Collection.
		/// </summary>
		/// <param name="value">Product object.</param>
		public void Remove (Account value) 
		{
			List.Remove(value);
		}
		
		#endregion
				
		#region "Typed Event Handlers"
		
		private ListChangedEventArgs resetEvent = new ListChangedEventArgs(ListChangedType.Reset, -1);
		private ListChangedEventHandler onListChanged;
	
	
		/// <summary>
		/// Raises the ListChanged event.
		/// </summary>
		/// <param name="ev">A <see cref="ListChangedEventArgs"/> that contains the event data.</param>
		protected virtual void OnListChanged(ListChangedEventArgs ev) 
		{
			if (onListChanged != null) 
			{
				onListChanged(this, ev);
			}
		}
	
	
		/// <summary>
		/// Raises the ListChanged event.
		/// </summary>
		protected override void OnClearComplete() 
		{
			if(_issorted)
				_OriginalList.Clear();
			OnListChanged(resetEvent);
		}
	
	
		/// <summary>
		/// Raises the InsertComplete event.
		/// Performs additional custom processes after inserting a new element into the AccountCollectionBase 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 OnInsertComplete(int index, object value) 
		{
			if(_issorted)
				_OriginalList.Add(value);
			OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
		}
	
	
		/// <summary>
		/// Raises the RemoveComplete event.
		/// Performs additional custom processes after removing a new element into the AccountCollectionBase 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 OnRemoveComplete(int index, object value) 
		{
			if(_issorted)
				_OriginalList.Remove(value);
			OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
		}
		
		
		/// <summary>
		/// Raises the SetComplete event.
		/// Performs additional custom processes after setting a value in the AccountCollectionBase 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 OnSetComplete(int index, object oldValue, object newValue) 
		{
			if (oldValue != newValue) 
			{           
				OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
			}
		}
	    #endregion "Typed Event Handlers"
				
		#region "IBindingList"
				
		///<summary>
		/// Gets whether you can update items in the list.
		///</summary>
		bool IBindingList.AllowEdit 
		{ 
			get { return true ; }
		}
		
	
		///<summary>
		/// Gets whether you can add items to the list using <see cref="IBindingList.AddNew"/>.
		///</summary>
		bool IBindingList.AllowNew 
		{ 
			get { return true ; }
		}
	
	
		///<summary>
		/// Gets whether you can remove items from the list, using <see cref="IList.Remove"/> or <see cref="IList.RemoveAt"/>.
		///</summary>
		bool IBindingList.AllowRemove 
		{ 
			get { return true ; }
		}
	
	
		///<summary>
		/// Gets whether a <see cref="ListChanged"/> event is raised when the list changes or an item in the list changes.
		///</summary>
		bool IBindingList.SupportsChangeNotification 
		{ 
			get { return true ; }
		}
	    
	    
		///<summary>
		/// Gets whether the list supports searching using the <see cref="IBindingList.Find"/> method.
		///</summary>
		bool IBindingList.SupportsSearching 
		{ 
			get { return true ; }
		}
	
		
		///<summary>
		/// Gets whether the list supports sorting.
		///</summary>
		bool IBindingList.SupportsSorting 
		{ 
			get { return true ; }
		}
		
		
		///<summary>
		/// Gets whether the items in the list are sorted.
		///</summary>
		bool IBindingList.IsSorted 
		{ 
			get { return _issorted; }
		}
	
	
		///<summary>
		/// Gets the direction of the sort.
		///</summary>
		ListSortDirection IBindingList.SortDirection 
		{ 
			get { return _sortdirection; }
		}
	
	
		///<summary>
		/// Gets the <see cref="PropertyDescriptor"/> that is being used for sorting.
		///</summary>
		PropertyDescriptor IBindingList.SortProperty 
		{ 
			get { return _sortby; }
		}
	
		///<summary>
		/// Returns the index of the row that has the given <see cref="PropertyDescriptor"/>.
		///</summary>
		///<param name="property">The <see cref="PropertyDescriptor"/> to search on. </param>
		///<param name="key">The value of the property parameter to search for.</param>
		///<returns>The index of the row that has the given <see cref="PropertyDescriptor"/>.</returns>
		int IBindingList.Find(PropertyDescriptor property, object key) 
		{
			foreach(Account _Account in this.List)
			{
				if (property.GetValue(_Account).Equals(key))
					return this.List.IndexOf(_Account);
			}
			return -1;
		}
	
	
		///<summary>
		/// Occurs when the list managed by the <see cref="AccountCollection"/> changes.
		///</summary>
		public event ListChangedEventHandler ListChanged 
		{
			add 
			{
				onListChanged += value;
			}
			remove 
			{
				onListChanged -= value;
			}
		}
		
		
		// Methods.
		
		///<summary>
		/// Adds a new item to the list.
		///</summary>
		///<returns>The item added to the list.</returns>
		object IBindingList.AddNew() 
		{
			Account c = new Account();
			List.Add(c);
			return c;
		}
		
		
		///<summary>
		/// Sorts the list based on a <see cref="PropertyDescriptor"/> and a <see cref="ListSortDirection"/>.
		///</summary>
		///<param name="property">The <see cref="PropertyDescriptor"/> to sort by.</param>
		///<param name="direction">One of the <see cref="ListSortDirection"/> values.</param>
		void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) 
		{
			_sortby = property;
			_sortdirection = direction;
			SortList();
		}
		
		
		///<summary>
		/// Removes any sort applied using <see cref="IBindingList.ApplySort"/>.
		///</summary>		
		public void RemoveSort() 
		{
			if(_issorted)
			{
				base.InnerList.Clear();
				foreach(object obj in _OriginalList)
					base.InnerList.Add(obj);
				_issorted = false;
				_OriginalList = null; //destroy
				OnListChanged(new ListChangedEventArgs(ListChangedType.Reset,0));
			}
		}
	
		#region "Unsupported Methods"
		
		///<summary>
		/// Adds the <see cref="PropertyDescriptor"/> to the indexes used for searching.
		///</summary>
		///<param name="property">The <see cref="PropertyDescriptor"/> to add to the indexes used for searching.</param>
		///<remarks>The list must support this method. However, support for this method can be a nonoperation.</remarks>
		///<exception cref="System.NotSupportedException">Thrown</exception>
		void IBindingList.AddIndex(PropertyDescriptor property) 
		{
			throw new NotSupportedException(); 
		}
		
		///<summary>
		/// Removes the <see cref="PropertyDescriptor"/> from the indexes used for searching.
		///</summary>
		///<param name="property">The <see cref="PropertyDescriptor"/> to remove from the indexes used for searching.</param>
		///<exception cref="System.NotSupportedException">Thrown</exception>
		void IBindingList.RemoveIndex(PropertyDescriptor property) 
		{
			throw new NotSupportedException(); 
		}
		#endregion "Unsupported Methods"
	
		#endregion "IBindingList"
				
		#region "ICloneable"
		
		///<summary>
		/// Creates an exact copy of this <see cref="AccountCollection"/> object.
		///</summary>
		///<returns>The <see cref="AccountCollection"/> object this method creates, cast as an object.</returns>
		///<implements><see cref="ICloneable.Clone"/></implements>
		public object Clone(){
			return this.Copy();
		}
		
		
		///<summary>
		/// Creates an exact copy of this <see cref="AccountCollection"/> object.
		///</summary>
		///<returns>A new, identical copy of the <see cref="AccountCollection"/>.</returns>
		public virtual AccountCollection Copy(){
			AccountCollection copy = new AccountCollection();
			foreach(Account _Account in this.List)
			{
				Account copyAccount = (Account)MakeCopyOf(_Account);
				copy.Add(copyAccount);	
			}
			return copy;
		}
		
		
		///<summary>
		/// Creates an exact copy of this <see cref="AccountCollection"/> object.
		///</summary>
		///<returns>A new, identical copy of the <see cref="AccountCollection"/> casted as object.</returns>
		public static object MakeCopyOf(object x)
		{
			if (x is ICloneable)
			{
				// Return a deep copy of the object
				return ((ICloneable)x).Clone();
			}
			else
			{
				throw new 
					System.NotSupportedException("object not cloneable");
			}
		}
		#endregion "ICloneable"
		
		#region "Added Functionality"
		
		/// <summary>
		///		Returns the number of items that have been marked new in the collection.
		/// </summary>
		///<returns>the number of items that have been marked new in the collection</returns>
		public int IsNewCount
		{
			get
			{
				int count = 0;
				foreach(Account p in this.List)
				{
					if(p.IsNew)
						count += 1;
				}
				return count;
			}
		}
		
		
		/// <summary>
		///		Returns the number of items that have been marked as modified in the collection.
		/// </summary>
		///<returns>the number of items that have been marked as modified in the collection</returns>
		public int IsDirtyCount
		{
			get
			{
				int count = 0;
				foreach(Account p in this.List)
				{
					if(p.IsDirty)
						count += 1;
				}
				return count;
			}
		}
		#endregion	"Added Functionality"
		
		///<summary>
		/// Returns a String that represents the current AccountCollection.
		///</summary>
		public override string ToString()
		{
			string output = string.Format("There is {0} Account object in this collection{1}{1}", this.List.Count, Environment.NewLine);
			foreach(Account p in this.List)
			{
				output += p.ToString();
			}
			return output;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions