Click here to Skip to main content
15,892,298 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="Student"/> objects that support sorting and binding.
	/// <remarks>
	/// It implements the IClonable, IBindingList and IList interfaces.
	/// </remarks>
	///</summary>
	[Serializable]
	public class StudentCollectionBase : 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="Student" /> 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 Student Find(StudentColumns searchfield, object searchvalue)
		{
			PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(Student));
			PropertyDescriptor searchby = null;
			switch(searchfield)
			{
				case StudentColumns.ID:
					searchby = props["ID"];
					break;
				case StudentColumns.FirstName:
					searchby = props["FirstName"];
					break;
				case StudentColumns.MiddleName:
					searchby = props["MiddleName"];
					break;
				case StudentColumns.LastName:
					searchby = props["LastName"];
					break;
				case StudentColumns.PresentAddress1:
					searchby = props["PresentAddress1"];
					break;
				case StudentColumns.PresentAddress2:
					searchby = props["PresentAddress2"];
					break;
				case StudentColumns.PermanentAddress1:
					searchby = props["PermanentAddress1"];
					break;
				case StudentColumns.PermanentAddress2:
					searchby = props["PermanentAddress2"];
					break;
				case StudentColumns.StudentID:
					searchby = props["StudentID"];
					break;
				case StudentColumns.Status:
					searchby = props["Status"];
					break;
				case StudentColumns.CGPA:
					searchby = props["CGPA"];
					break;
				case StudentColumns.CreditCompleted:
					searchby = props["CreditCompleted"];
					break;
				case StudentColumns.DateAdmitted:
					searchby = props["DateAdmitted"];
					break;
				case StudentColumns.DateGraduated:
					searchby = props["DateGraduated"];
					break;
				case StudentColumns.ChangeStamp:
					searchby = props["ChangeStamp"];
					break;
			}
			int j = ((IBindingList)this).Find(searchby, searchvalue);
			if (j > -1)
				return this[j];
			else
				return null;
		}
		
		///<summary>
		/// Finds a collection of <see cref="Student" /> objects in the current list.
		///</summary>
		/// <param name="searchfield">Field of the object to search.</param>
		/// <param name="searchvalue">Value to find.</param>
		public StudentCollection FindAll(StudentColumns searchfield, object searchvalue)
		{
			PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(Student));
			PropertyDescriptor searchby = null;
			switch(searchfield)
			{
				case StudentColumns.ID:
					searchby = props["ID"];
					break;
				case StudentColumns.FirstName:
					searchby = props["FirstName"];
					break;
				case StudentColumns.MiddleName:
					searchby = props["MiddleName"];
					break;
				case StudentColumns.LastName:
					searchby = props["LastName"];
					break;
				case StudentColumns.PresentAddress1:
					searchby = props["PresentAddress1"];
					break;
				case StudentColumns.PresentAddress2:
					searchby = props["PresentAddress2"];
					break;
				case StudentColumns.PermanentAddress1:
					searchby = props["PermanentAddress1"];
					break;
				case StudentColumns.PermanentAddress2:
					searchby = props["PermanentAddress2"];
					break;
				case StudentColumns.StudentID:
					searchby = props["StudentID"];
					break;
				case StudentColumns.Status:
					searchby = props["Status"];
					break;
				case StudentColumns.CGPA:
					searchby = props["CGPA"];
					break;
				case StudentColumns.CreditCompleted:
					searchby = props["CreditCompleted"];
					break;
				case StudentColumns.DateAdmitted:
					searchby = props["DateAdmitted"];
					break;
				case StudentColumns.DateGraduated:
					searchby = props["DateGraduated"];
					break;
				case StudentColumns.ChangeStamp:
					searchby = props["ChangeStamp"];
					break;
			}
	
			StudentCollection copy = new StudentCollection();
			foreach(Student _Student in this.List)
			{
				if (searchby.GetValue(_Student).Equals(searchvalue))
				{
					Student copyStudent = (Student)MakeCopyOf(_Student);
					copy.Add(copyStudent);	
				}
			}
			return copy;
		}

		#endregion
	
		#region "Sort Code"
		
		///<summary>
		/// Sorts the elements in the StudentCollectionBase .
		///</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(StudentColumns field, ListSortDirection direction)
		{
			this._sortby = TypeDescriptor.GetProperties(typeof(Student)).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 Student instance to the Collection.
		/// </summary>
		/// <param name="value"><see cref="Product"/> instance.</param>
		/// <returns></returns>
		public int Add (Student value) 
		{
			return List.Add(value);
		}
		
		/// <summary>
		///	Adds a new Student instance to the Collection.
		/// </summary>
		///<param name="ID"></param>
		///<param name="FirstName"></param>
		///<param name="MiddleName"></param>
		///<param name="LastName"></param>
		///<param name="PresentAddress1"></param>
		///<param name="PresentAddress2"></param>
		///<param name="PermanentAddress1"></param>
		///<param name="PermanentAddress2"></param>
		///<param name="StudentID"></param>
		///<param name="Status"></param>
		///<param name="CGPA"></param>
		///<param name="CreditCompleted"></param>
		///<param name="DateAdmitted"></param>
		///<param name="DateGraduated"></param>
		///<param name="ChangeStamp"></param>
		/// <returns></returns>
		public int Add (System.Int32 ID, System.String FirstName, System.String MiddleName, System.String LastName, System.String PresentAddress1, System.String PresentAddress2, System.String PermanentAddress1, System.String PermanentAddress2, System.String StudentID, System.Int32 Status, System.Single CGPA, System.Int32 CreditCompleted, System.DateTime DateAdmitted, System.DateTime DateGraduated, System.DateTime ChangeStamp) 
		{
			return List.Add(new Student(ID, FirstName, MiddleName, LastName, PresentAddress1, PresentAddress2, PermanentAddress1, PermanentAddress2, StudentID, Status, CGPA, CreditCompleted, DateAdmitted, DateGraduated, ChangeStamp));
		}
		/// <summary>
		///	Adds a new Student to the Collection.
		/// </summary>
		/// <returns>Product object.</returns>
		public Student AddNew() 
		{
			return (Student)((IBindingList)this).AddNew();
		}
		
		
		/// <summary>
		/// Gets or sets the <see cref="Student"/> at the specified index.
		/// </summary>
		/// <value></value>
		public Student this[int index] 
		{
			get 
			{
				return (Student)(List[index]);
			}
			set 
			{
				List[index] = value;
			}
		}
		
		
		/// <summary>
		///	Removes a Student object from the Collection.
		/// </summary>
		/// <param name="value">Product object.</param>
		public void Remove (Student 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 StudentCollectionBase 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 StudentCollectionBase 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 StudentCollectionBase 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(Student _Student in this.List)
			{
				if (property.GetValue(_Student).Equals(key))
					return this.List.IndexOf(_Student);
			}
			return -1;
		}
	
	
		///<summary>
		/// Occurs when the list managed by the <see cref="StudentCollection"/> 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() 
		{
			Student c = new Student();
			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="StudentCollection"/> object.
		///</summary>
		///<returns>The <see cref="StudentCollection"/> 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="StudentCollection"/> object.
		///</summary>
		///<returns>A new, identical copy of the <see cref="StudentCollection"/>.</returns>
		public virtual StudentCollection Copy(){
			StudentCollection copy = new StudentCollection();
			foreach(Student _Student in this.List)
			{
				Student copyStudent = (Student)MakeCopyOf(_Student);
				copy.Add(copyStudent);	
			}
			return copy;
		}
		
		
		///<summary>
		/// Creates an exact copy of this <see cref="StudentCollection"/> object.
		///</summary>
		///<returns>A new, identical copy of the <see cref="StudentCollection"/> 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(Student 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(Student p in this.List)
				{
					if(p.IsDirty)
						count += 1;
				}
				return count;
			}
		}
		#endregion	"Added Functionality"
		
		///<summary>
		/// Returns a String that represents the current StudentCollection.
		///</summary>
		public override string ToString()
		{
			string output = string.Format("There is {0} Student object in this collection{1}{1}", this.List.Count, Environment.NewLine);
			foreach(Student 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