Click here to Skip to main content
15,880,392 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
	 
#region Using directives

using System;
using System.Data;
using System.Collections;
using System.Diagnostics;
using SmartInstitute;

#endregion

namespace SmartInstitute.DataAccessLayer.Factories
{

///<summary>
/// This class is an helper Repository that exposes CRUD methods for Permission objects as static methods.
/// It forward the process to the concrete IPermissionRepository that is defined in the configuration file.
///</summary>
public class PermissionRepository : IPermissionRepository
{
	private static volatile PermissionRepository current;
   	private static object syncRoot = new Object();
   	private IPermissionRepository repository;
	
	#region "Constructors"
		
	///<summary>
	/// Creates a new <see cref="PermissionRepository"/> instance.
	///</summary>
	private PermissionRepository()
	{
		this.repository = new DataAccessLayer.SqlClient.PermissionRepository(string.Empty);
	}
	
	///<summary>
	/// The current <see href="PermissionRepository"/> instance.
	///</summary>
	///<value></value>
	public static PermissionRepository Current
	{
	  get 
	  {
	     if (current == null) 
	     {
	        lock (syncRoot) 
	        {
	           if (current == null)
	           {
			   		current = new PermissionRepository();
			   }
	        }
	     }
	     return current;
	  }
	}
		
	#endregion "Constructors"
	
	#region Public Properties
	///<summary>
	/// The current IPermissionRepository instance, as configured in the DataAccessLayer/ClientType configuration section.
	///</summary>
	private IPermissionRepository Repository
	{
		get
		{
			return this.repository;
		}
	} 
	#endregion
	
	#region "Get from  Many To Many Relationship Functions"
	#endregion	
	
	
	#region "Delete Functions"
		
	/// <summary>
	/// 	Deletes rows from the DataSource.
	/// </summary>
	/// <param name="entityCollection">PermissionCollection containing data.</param>
	/// <remarks>Deletes Permissions only when IsDeleted equals true.</remarks>
	/// <returns>Returns the number of successful delete.</returns>
	public int Delete(PermissionCollection entityCollection)
	{
		int number = 0;
		foreach (Permission entity in entityCollection)
		{
			if( Delete(entity) )
			{
				number++;
			}
		}
		return number;
	}
		
	/// <summary>
	/// 	Deletes a row from the DataSource.
	/// </summary>
	/// <param name="entity">Permission object containing data.</param>
	/// <remarks>Deletes based on primary key(s).</remarks>
	/// <returns>Returns true if operation suceeded.</returns>
	public bool Delete(Permission entity)
	{
		return Delete(entity.OriginalID, entity.ChangeStamp);	
	}
					
	/// <summary>
	/// 	Deletes a row from the DataSource.
	/// </summary>
	/// <param name="ID">. Primary Key.</param>	
	/// <remarks>Deletes based on primary key(s).</remarks>
	/// <returns>Returns true if operation suceeded.</returns>
	public bool Delete(System.Int32 ID, DateTime ChangeStamp)
	{
		return Repository.Delete(ID, ChangeStamp);
	}//end Delete
	
	
	/// <summary>
	/// Throws the delete concurrency exception.
	/// </summary>
	/// <param name="ID">. Primary Key.</param>	
	protected static void ThrowDeleteConcurrencyException(System.Int32 ID, DateTime ChangeStamp)
	{
		DBConcurrencyException conflict = new DBConcurrencyException("Concurrency exception: Cannot delete entity as it does not exist.");
		//conflict.ModifiedRecord = entity;
		throw conflict;
	}	
	
	#endregion
	
		
	#region "GetList Functions"
	
	/// <summary>
	/// 	Gets All rows from the DataSource.
	/// </summary>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetAll()
	{
		return GetAll(0, int.MaxValue);
	}
	
	/// <summary>
	/// 	Gets All rows from the DataSource.
	/// </summary>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pageLength">Number of rows to return.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetAll(int start, int pageLength)
	{
		return Repository.GetAll(start, pageLength);
	}
	
	#endregion
	
	#region Paged Recordset
	
	/// <summary>
	/// Gets a page of rows from the DataSource.
	/// </summary>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pageLength">Number of rows to return.</param>
	/// <param name="count">Number of rows in the DataSource.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetPaged(int start, int pageLength, out int count)
	{
		return Repository.GetPaged(null, null, start, pageLength, out count);
	}
	
	/// <summary>
	/// Gets a page of rows from the DataSource.
	/// </summary>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pageLength">Number of rows to return.</param>
	/// <param name="count">Number of rows in the DataSource.</param>
	/// <param name="whereClause">Specifies the condition for the rows returned by a query (Name='John Doe', Name='John Doe' AND Id='1', Name='John Doe' OR Id='1').</param>
	/// <param name="orderBy">Specifies the sort criteria for the rows in the DataSource (Name ASC; BirthDay DESC, Name ASC);</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetPaged(string whereClause, string orderBy, int start, int pageLength, out int count)
	{
		return Repository.GetPaged(whereClause, orderBy, start, pageLength, out count);
	}
	
	#endregion
		
		
	#region "Get By Foreign Key Functions"
	#endregion
	
	
	#region "Get By Index Functions"

	
	/// <summary>
	/// 	Gets rows from the datasource based on the PK_Permission index.
	/// </summary>
	/// <param name="ID"></param>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetByID(System.Int32 ID)
	{
		return GetByID(ID, 0, int.MaxValue);
	}	
	

	/// <summary>
	/// 	Gets rows from the datasource based on the PK_Permission index.
	/// </summary>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pageLength">Number of rows to return.</param>
	/// <param name="ID"></param>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetByID(System.Int32 ID, int start, int pageLength)
	{		
		return Repository.GetByID(ID, start, pageLength);
	}	

	#endregion "Get By Index Functions"


	#region "Insert Functions"
		
	/// <summary>
	/// 	Insert rows in the datasource.
	/// </summary>
	/// <param name="entityCollection"><c>Permission</c> objects in a <c>PermissionCollection</c> object to insert.</param>
	/// <remarks>
	///		This function will only insert entity objects marked as dirty
	///		and have an identity field equal to zero.
	///		Upon inserting the objects, each dirty object will have the public
	///		method <c>Object.AcceptChanges()</c> called to make it clean.
	/// 	After inserting into the datasource, the <c>Permission</c> objects will be updated
	/// 	to refelect any changes made by the datasource. (ie: identity columns)</remarks>
	/// <returns>Returns the number of successful insert.</returns>
	public int Insert(PermissionCollection entityCollection)
	{
		int number = 0;
		//Extract only dirty objects to save time and bandwidth
		foreach (Permission entity in entityCollection)
		{
			if (entity.IsNew)
			{
				if ( Insert(entity) )
				{
					number++;
				}
			}
		}
		return number;
	}

	/// <summary>
	/// 	Inserts a Permission object into the datasource using a transaction.
	/// </summary>
	/// <param name="entity">Permission object to insert.</param>
	/// <remarks>After inserting into the datasource, the Permission object will be updated
	/// to refelect any changes made by the datasource. (ie: identity columns)</remarks>
	/// <returns>Returns true if operation is successful.</returns>
	public bool Insert(Permission entity)
	{
		return Repository.Insert(entity);
	}	
	#endregion


	#region "Update Functions"
		
	/// <summary>
	/// 	Update existing rows in the datasource.
	/// </summary>
	/// <param name="entityCollection"><c>Permission</c> objects in a <c>PermissionCollection</c> object to update.</param>
	/// <remarks>
	///		This function will only update entity objects marked as dirty
	///		and do not have an primary key value of 0.
	///		Upon updating the objects, each dirty object will have the public
	///		method <c>Object.AcceptChanges()</c> called to make it clean.
	/// 	After updating the datasource, the <c>Permission</c> objects will be updated
	/// 	to refelect any changes made by the datasource. (ie: identity columns)</remarks>
	/// <returns>Returns the number of successful update.</returns>
	public int Update(PermissionCollection entityCollection)
	{	
		int number = 0;
		foreach (Permission entity in entityCollection)
		{
			if ((entity.IsDirty) && !(entity.IsNew))
			{
				if ( Update(entity) )
				{
					number++;
				}
			}
		}
		return number;
	}
	
	/// <summary>
	/// 	Update an existing row in the datasource.
	/// </summary>
	/// <param name="entity">Permission object to update.</param>
	/// <remarks>After updating the datasource, the Permission object will be updated
	/// to refelect any changes made by the datasource. (ie: identity columns)</remarks>
	/// <returns>Returns true if operation is successful.</returns>
	public bool Update(Permission entity)
	{
		return Repository.Update(entity);
	}
	#endregion


	#region "Save Functions"
	
	/// <summary>
	/// 	Save rows changes in the datasource (insert, update ,delete).
	/// </summary>
	/// <param name="entity">Permission object to update.</param>
	/// <remarks>
	/// 	After updating the datasource, the <c>Permission</c> objects will be updated or inserted
	/// 	to refelect any changes made by the datasource. (ie: identity columns)</remarks>
	/// <returns>Returns true if operation is successful.</returns>
	public void Save(Permission entity)
	{		
		if (entity.IsDeleted)
			Delete(entity);
			
		else if ((entity.IsDirty) && !(entity.IsNew))
			Update(entity);
			
		else if (entity.IsNew)
			Insert(entity);
	}
		
	/// <summary>
	/// 	Save rows changes in the datasource (insert, update ,delete).
	/// </summary>
	/// <param name="entityCollection"><c>Permission</c> objects in a <c>PermissionCollection</c> object to save.</param>
	/// <remarks>
	/// 	After updating the datasource, the <c>Permission</c> objects will be updated or inserted
	/// 	to refelect any changes made by the datasource. (ie: identity columns)</remarks>
	/// <returns>Returns true if operation is successful.</returns>
	public void Save(PermissionCollection entityCollection)
	{
		foreach (Permission entity in entityCollection)
		{			
			Save(entity);
		}
	}
	#endregion


	#region "Helper Functions"	
	
	///<summary>
	/// Fill an PermissionCollection From a DataSet
	///</summary>
	/// <param name="dataSet">the DataSet</param>
	/// <param name="rows">The collection to fill</param>
	/// <param name="start">Start row</param>
	/// <param name="pageLength">number of row.</param>
	///<returns><see chref="PermissionCollection"/></returns>
	protected static PermissionCollection Fill(DataSet dataSet, PermissionCollection rows, int start, int pageLength)
	{
		int recordnum = 0;
		
		System.Collections.IEnumerator dataRows =  dataSet.Tables[0].Rows.GetEnumerator();
		
		while (dataRows.MoveNext() && (pageLength != 0))
		{
			if(recordnum >= start)
			{
				DataRow row = (DataRow)dataRows.Current;
			
				Permission c = new Permission();
				c.ID = (Convert.IsDBNull(row["ID"]))?(int)0:(System.Int32)row["ID"];
				c.OriginalID = (Convert.IsDBNull(row["ID"]))?(int)0:(System.Int32)row["ID"];
				c.Title = (Convert.IsDBNull(row["Title"]))?string.Empty:(System.String)row["Title"];
				c.Description = (Convert.IsDBNull(row["Description"]))?string.Empty:(System.String)row["Description"];
				c.ChangeStamp = (Convert.IsDBNull(row["ChangeStamp"]))?DateTime.MinValue:(System.DateTime)row["ChangeStamp"];
				c.AcceptChanges();
				rows.Add(c);
				pageLength -= 1;
			}
			recordnum += 1;
		}
		return rows;
	}
			
	
	
	/// <summary>
	/// Refreshes the <see cref="Permission"/> object from the <see cref="DataSet"/>.
	/// </summary>
	/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
	/// <param name="entity">The <see cref="Permission"/> object.</param>
	protected static void RefreshEntity(DataSet dataSet, Permission entity)
	{
		DataRow dataRow = dataSet.Tables[0].Rows[0];
		
		entity.ID = (Convert.IsDBNull(dataRow["ID"]))?(int)0:(System.Int32)dataRow["ID"];
		entity.OriginalID = (Convert.IsDBNull(dataRow["ID"]))?(int)0:(System.Int32)dataRow["ID"];
		entity.Title = (Convert.IsDBNull(dataRow["Title"]))?string.Empty:(System.String)dataRow["Title"];
		entity.Description = (Convert.IsDBNull(dataRow["Description"]))?string.Empty:(System.String)dataRow["Description"];
		entity.ChangeStamp = (Convert.IsDBNull(dataRow["ChangeStamp"]))?DateTime.MinValue:(System.DateTime)dataRow["ChangeStamp"];
		entity.AcceptChanges();
	}
		
	#region DeepLoad
	#region Deep Load By Entity
	/// <summary>
	/// Deep Load the IEntity object with all of the child 
	/// property collections only 1 Level Deep.
	/// </summary>
	/// <remarks>
	/// <seealso cref="DeepLoad"/> overloaded methods for a recursive N Level deep loading method.
	/// </remarks>
	/// <param name="entity">Permission Object</param>
	public void DeepLoad(Permission entity)
	{
	 	DeepLoad(entity, false, DeepLoadType.ExcludeChildren, new Type[] {});
	}
	
	/// <summary>
	/// Deep Load the IEntity object with all of the child 
	/// property collections only 1 Level Deep.
	/// </summary>
	/// <remarks>
	/// <seealso cref="DeepLoad"/> overloaded methods for a recursive N Level deep loading method.
	/// </remarks>
	/// <param name="entity">Permission Object</param>
	/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
	public void DeepLoad(Permission entity, bool deep)
	{
	 	DeepLoad(entity, deep, DeepLoadType.ExcludeChildren, new Type[] {});
	}
	
	/// <summary>
	/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
	/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
	/// </summary>
	/// <remarks>
	/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
	/// </remarks>
	/// <param name="entity">The <see cref="Permission"/> object to load.</param>
	/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
	/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
	/// <param name="childTypes">Permission Property Collection Type Array To Include or Exclude from Load</param>
	public void DeepLoad(Permission entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes)
	{
		#region Argument Validation
		//Argument checks
		if (entity ==  null)
		{
			throw new ArgumentNullException("The argument Permission, can not be null.");
		}
		if (!Enum.IsDefined(typeof(DeepLoadType), deepLoadType))
		{
			throw new ArgumentNullException("A valid DeepLoadType option is not present.");
		}
		if (childTypes == null)
		{
			throw new ArgumentNullException("A valid Type[] array is not present.");
		}
		#endregion
		
		//In case an event can trigger the disabling of the deep load
		if (deepLoadType == DeepLoadType.Ignore)
		{
			return;
		}
		
		//Create a HashTable list of types for easy access
		Hashtable innerList = new Hashtable(childTypes.Length);
		for(int i=0; i < childTypes.Length; i++)
		{
			innerList.Add(childTypes[i], childTypes[i].ToString()); 
		}
		
		Debug.Indent();
		Debug.WriteLine("DeepLoad object 'Permission'");
		Debug.Indent();
		
		// Load Entity through Provider
		Debug.Unindent();
		Debug.Unindent();
		Debug.WriteLine("");
		return;
	}
	
	#endregion
	
	#region Deep Load By Entity Collection
	/// <summary>
	/// Deep Loads the <see cref="PermissionCollection"/> object with all of the child 
	/// property collections only 1 Level Deep.
	/// </summary>
	/// <remarks>
	/// <seealso cref="DeepLoad"/> overloaded methods for a recursive N Level deep loading method.
	/// </remarks>
	/// <param name="entityCollection">the <see cref="PermissionCollection"/> Object to deep loads.</param>
	public void DeepLoad(PermissionCollection entityCollection)
	{
		 DeepLoad(entityCollection, false, DeepLoadType.ExcludeChildren, new Type[] {});
	}
	
	/// <summary>
	/// Deep Loads the <see cref="PermissionCollection"/> object.
	/// </summary>
	/// <remarks>
	/// <seealso cref="DeepLoad"/> overloaded methods for a recursive N Level deep loading method.
	/// </remarks>
	/// <param name="entityCollection">the <see cref="PermissionCollection"/> Object to deep loads.</param>
	/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
	public void DeepLoad(PermissionCollection entityCollection, bool deep)
	{
		 DeepLoad(entityCollection, deep, DeepLoadType.ExcludeChildren, new Type[] {});
	}	

	/// <summary>
	/// Deep Loads the entire <see cref="PermissionCollection"/> object with criteria based of the child 
	/// property collections only N Levels Deep based on the DeepLoadType.
	/// </summary>
	/// <remarks>
	/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire collection's object graph.
	/// </remarks>
	/// <param name="entityCollection">The <see cref="PermissionCollection"/> instance to load.</param>
	/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
	/// <param name="deepLoadType"><see cref="DeepLoadType"/> Enumeration to Include/Exclude object property collections from Load.
	///		Use DeepLoadType.[IncludeChildren/ExcludeChildren]WithRecursion to traverse the entire object graph.
	///	</param>
	/// <param name="childTypes"><see cref="Permission"/> Property Collection Type Array To Include or Exclude from Load</param>
	public void DeepLoad(PermissionCollection entityCollection, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes)
	{
		#region Argument Validation
		//Argument checks
		if (entityCollection ==  null)
		{
			throw new ArgumentNullException("A valid non-null, PermissionCollection object is not present.");
		}
		if (!Enum.IsDefined(typeof(DeepLoadType), deepLoadType))
		{
			throw new ArgumentNullException("A valid DeepLoadType option is not present.");
		}
		if (childTypes == null)
		{
			throw new ArgumentNullException("A valid Type[] array is not present.");
		}
		#endregion
				
		//In case an event can trigger the disabling of the deepload
		if (deepLoadType == DeepLoadType.Ignore)
		{
			return;
		}
		
		foreach (Permission entity in entityCollection)
		{
			DeepLoad(entity, deep, deepLoadType, childTypes);
		}		
		return;
	}
	#endregion
	#endregion 
	
	#region DeepSave
	
	#region Deep Save By Entity
	
	/// <summary>
	/// Deep Save the <see cref="Permission"/> object with all of the child
	/// property collections N Levels Deep.
	/// </summary>
	/// <param name="entity">Permission Object</param>
	public bool DeepSave(Permission entity)
	{
		return DeepSave(entity, DeepSaveType.ExcludeChildren, new Type[] {} );
	}
				
	/// <summary>
	/// Deep Save the entire object graph of the Permission object with criteria based of the child 
	/// Type property array and DeepSaveType.
	/// </summary>
	/// <param name="entity">Permission Object</param>
	/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.
	///	</param>
	/// <param name="childTypes">Permission Property Collection Type Array To Include or Exclude from Save</param>
	public bool DeepSave(Permission entity, DeepSaveType deepSaveType, System.Type[] childTypes)
	{
		#region Argument Validation
		//Argument checks
		if (entity ==  null)
		{
			throw new ArgumentNullException("The argument Permission, can not be null.");
		}
		if (!Enum.IsDefined(typeof(DeepSaveType), deepSaveType))
		{
			throw new ArgumentNullException("A valid DeepSaveType option is not present.");
		}
		if (childTypes == null)
		{
			throw new ArgumentNullException("A valid Type[] array is not present.");
		}
		#endregion
		
		//In case an event can trigger the disabling of the deepsave
		if (deepSaveType == DeepSaveType.Ignore)
		{
			return true;
		}
		
		//Create a HashTable list of types for easy access
		Hashtable innerList = new Hashtable(childTypes.Length);
		for(int i=0; i < childTypes.Length; i++)
		{
			innerList.Add(childTypes[i], childTypes[i].ToString()); 
		}

		// Save Root Entity through Provider
		PermissionRepository rep = new PermissionRepository();
		rep.Save(entity);
		
		return true;
	}
	#endregion
	
	#region Deep Save By Entity Collection
	/// <summary>
	/// Deep Save the entire PermissionCollection object with all of the child 
	/// property collections.
	/// </summary>
	/// <param name="entityCollection">PermissionCollection Object</param>
	public bool DeepSave(PermissionCollection entityCollection)
	{
		return DeepSave(entityCollection, DeepSaveType.ExcludeChildren, new Type[] {});
	}
	
	
	/// <summary>
	/// Deep Save the entire object graph of the PermissionCollection object with criteria based of the child 
	/// property collections.
	/// </summary>
	/// <param name="entityCollection">PermissionCollection Object</param>
	/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
	/// <param name="childTypes">Permission Property Collection Type Array To Include or Exclude from Save</param>
	public bool DeepSave(PermissionCollection entityCollection, DeepSaveType deepSaveType, System.Type[] childTypes)
	{
		#region Argument Validation
		//Argument checks
		if (entityCollection ==  null)
		{
			throw new ArgumentNullException("A valid non-null, PermissionCollection object is not present.");
		}
		if (!Enum.IsDefined(typeof(DeepSaveType), deepSaveType))
		{
			throw new ArgumentNullException("A valid DeepSaveType option is not present.");
		}
		if (childTypes == null)
		{
			throw new ArgumentNullException("A valid Type[] array is not present.");
		}
		#endregion
				
		//In case an event can trigger the disabling of the deepsave
		if (deepSaveType == DeepSaveType.Ignore)
		{
			return true;
		}
		
		bool deepSaveResult = true;
		bool result;
		
		foreach (Permission entity in entityCollection)
		{
			result = DeepSave(entity, deepSaveType, childTypes);
			if (!result){
				 deepSaveResult = false;
			}
		}
		
		return deepSaveResult;
	}
	#endregion
	
	#endregion
		
	#endregion "Helper Functions"
	
}//end class
} // end namespace

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