Click here to Skip to main content
15,881,424 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.Data.SqlClient;
//using Microsoft.ApplicationBlocks.Data;
using System.Collections;
using System.Diagnostics;
using SmartInstitute;

#endregion

namespace SmartInstitute.DataAccessLayer.SqlClient
{

/// <summary>
///	This class is the base repository for the CRUD operations on the Permission objects.
/// </summary>
public class PermissionRepositoryBase : IPermissionRepository
{
	#region "Declarations"	
	
	/// <summary>
	/// Connection String.
	/// </summary>
	protected string connectionString = string.Empty;
	
	
	/// <summary>
	/// <see cref="TransactionManager"/> object.
	/// </summary>
	protected TransactionManager transactionManager;
	
	private static volatile PermissionRepositoryBase current;
   	private static object syncRoot = new Object();
	
	#endregion "Declarations"
	
	
	#region "Constructors"
		
	/// <summary>
	/// Creates a new <see cref="PermissionRepositoryBase"/> instance.
	/// Uses connection string to connect to datasource.
	/// </summary>
	/// <param name="connectionString">Connection string.</param>
	protected PermissionRepositoryBase(string connectionString)
	{
		this.connectionString = connectionString;
	}
	
	/// <summary>
	///	Creates a new <see cref="PermissionRepositoryBase"/> instance.
	/// Uses connection string to connect to datasource.
	/// If a transaction is open, it will use the transaction, otherwise it will use the connection string from the transaction manager object.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object.</param>
	protected PermissionRepositoryBase(TransactionManager transactionManager)
	{
		this.transactionManager = transactionManager;
		this.connectionString = this.transactionManager.ConnectionString;
	}
	
	#endregion "Constructors"
	
	#region Public properties
	
	///<summary>
	/// The current PermissionRepositoryBase instance.
	///</summary>
	///<value></value>
	public static PermissionRepositoryBase Current
	{
	  get 
	  {
	     if (current == null) 
	     {
	        lock (syncRoot) 
	        {
	           if (current == null)
	           {
			   		current = new PermissionRepositoryBase(string.Empty);
			   }
	        }
	     }
	     return current;
	  }
	}
	
	///<summary>
	/// Gets or sets the connectionstring to the database.
	///</summary>
	///<value></value>
	public string ConnectionString
	{
		get {return this.connectionString;}
		set {this.connectionString = value;}
	}
	
	
	///<summary>
	/// Gets or sets the TransactionManager instance.
	///</summary>
	///<value></value>
	public TransactionManager TransactionManager
	{
		get {return this.transactionManager;}
		set {this.transactionManager = value;}
	}
	
	#endregion
	
	#region "Get from  Many To Many Relationship Functions"
	#endregion
	
	#region "Delete Functions"
	
	/// <summary>
	/// 	Deletes a row from the DataSource.
	/// </summary>
	/// <param name="ID">. Primary Key.</param>	
    /// <param name="ChangeStamp">Concurrency Parameter. </param>
	/// <returns>Returns true if operation suceeded.</returns>
	public bool Delete(System.Int32 ID, DateTime ChangeStamp)
	{
		if(UseTransaction())
		{
			return Delete(this.transactionManager, ID, ChangeStamp);
		}
		else
		{
			return Delete(this.connectionString, ID, ChangeStamp);
		}
	}//end Delete
	
	/// <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)
	{
		if(UseTransaction())
		{
			return Delete(this.transactionManager, entity.OriginalID, entity.ChangeStamp);	
		}
		else
		{
			return Delete(this.connectionString, entity.OriginalID, entity.ChangeStamp);	
		}
	}//end Delete
	/// <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)
	{
		if(UseTransaction())
			return Delete(this.transactionManager, entityCollection);
		else
			return Delete(this.connectionString, entityCollection);
	}
	
	
	/// <summary>
	/// 	Deletes a rows from the DataSource.
	/// </summary>
	/// <param name="entityCollection">PermissionCollection containing data.</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks>Deletes Permissions only when IsDeleted equals true.</remarks>
	/// <returns>Returns the number of successful delete.</returns>
	public int Delete(string connectionString, PermissionCollection entityCollection)
	{
		int number = 0;
		foreach (Permission entity in entityCollection)
		{
			if ( Delete(connectionString, entity) )
			{
				number++;
			}
		}
		return number;
	}
	
	/// <summary>
	/// 	Deletes a rows from the DataSource.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <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(TransactionManager transactionManager, PermissionCollection entityCollection)
	{
		int number = 0;
		foreach (Permission entity in entityCollection)
		{
			if ( Delete(transactionManager, entity) )
			{
				number++;
			}
		}
		return number;
	}
	
	
	/// <summary>
	/// 	Deletes a row from the DataSource.
	/// </summary>
	/// <param name="entity">Permission object containing data.</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks>Deletes based on primary key(s).</remarks>
	/// <returns>Returns true if operation suceeded.</returns>
	public bool Delete(string connectionString, Permission entity)
	{
		return Delete(null, connectionString,entity.OriginalID, entity.ChangeStamp);	
	}
	
	
	/// <summary>
	/// 	Deletes a row from the DataSource.
	/// </summary>
	/// <param name="ID">. Primary Key.</param>	
	/// <param name="connectionString">Connection string to datasource.</param>
    /// <param name="ChangeStamp">Concurrency Parameter. </param>
	/// <remarks>Deletes based on primary key(s).</remarks>
	/// <returns>Returns true if operation suceeded.</returns>
	public bool Delete(string connectionString, System.Int32 ID, DateTime ChangeStamp)
	{
		return Delete(null, connectionString,ID, ChangeStamp);
	}
	
	
	/// <summary>
	/// 	Deletes a row from the DataSource.
	/// </summary>
	/// <param name="entity">Permission object containing data.</param>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <remarks>Deletes based on primary key(s).</remarks>
	/// <returns>Returns true if operation suceeded.</returns>
	public bool Delete(TransactionManager transactionManager, Permission entity)
	{
		if (transactionManager.IsOpen)
			return Delete(transactionManager, null, entity.OriginalID, entity.ChangeStamp);	
		else
			return Delete(null, transactionManager.ConnectionString, entity.OriginalID, entity.ChangeStamp);	
	}
	
	
	/// <summary>
	/// 	Deletes a row from the DataSource.
	/// </summary>
	/// <param name="ID">. Primary Key.</param>	
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
    /// <param name="ChangeStamp">Concurrency Parameter. </param>
	/// <remarks>Deletes based on primary key(s).</remarks>
	/// <returns>Returns true if operation suceeded.</returns>
	public bool Delete(TransactionManager transactionManager, System.Int32 ID, DateTime ChangeStamp)
	{
		if (transactionManager.IsOpen)
			return Delete(transactionManager, null, ID, ChangeStamp);
		else
			return Delete(null, transactionManager.ConnectionString, ID, ChangeStamp);
	}
	
	
	
	/// <summary>
	/// 	Deletes a row from the DataSource.
	/// </summary>
	/// <param name="ID">. Primary Key.</param>	
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks>Deletes based on primary key(s).</remarks>
	/// <returns>Returns true if operation suceeded.</returns>
	protected bool Delete(TransactionManager transactionManager, string connectionString, System.Int32 ID, DateTime ChangeStamp)
	{
		int result = 0;
		if (transactionManager != null)
			result = SqlHelper.ExecuteNonQuery(transactionManager.TransactionObject, "prc_Permission_Delete", ID, ChangeStamp);
		else
			result = SqlHelper.ExecuteNonQuery(connectionString, "prc_Permission_Delete", ID, ChangeStamp);
			
		Debug.WriteLine("PermissionRepository.Delete Affected " + result + " records.");
		if (result == 0) {
			ThrowDeleteConcurrencyException( ID, ChangeStamp);
		} 
		return Convert.ToBoolean(result);
	}//end Delete
	
	
	/// <summary>
	/// Throws the delete concurrency exception.
	/// </summary>
	/// <param name="ID">. Primary Key.</param>	
	protected 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>
	/// <remarks>Uses connection string object was created with.</remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetAll()
	{	
		if(UseTransaction())
		{
			return GetAll(this.transactionManager);
		}
		else
		{
			return GetAll(this.connectionString);
		}
	}
	
	/// <summary>
	/// 	Gets All rows from the DataSource.
	/// </summary>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pagelen">Number of rows to return.</param>
	/// <remarks>Uses connection string object was created with.</remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetAll(int start, int pagelen)
	{	
		if(UseTransaction())
		{
			return GetAll(this.transactionManager, start, pagelen);
		}
		else
		{
			return GetAll(this.connectionString, start, pagelen);
		}
	}
	
	
	/// <summary>
	/// 	Gets All rows from the DataSource.
	/// </summary>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetAll(string connectionString)
	{
		return GetAll(null, connectionString,0,int.MaxValue);
	}
	
	/// <summary>
	/// 	Gets All rows from the DataSource.
	/// </summary>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pagelen">Number of rows to return.</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetAll(string connectionString, int start, int pagelen)
	{
		return GetAll(null, connectionString, start, pagelen);
	}//end getall
	
	
	/// <summary>
	/// 	Gets All rows from the DataSource.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetAll(TransactionManager transactionManager)
	{
		if (transactionManager.IsOpen)
			return GetAll(transactionManager, null, 0,int.MaxValue);
		else
			return GetAll(null, transactionManager.ConnectionString, 0,int.MaxValue);
	}
	
	
	/// <summary>
	/// 	Gets All rows from the DataSource.
	/// </summary>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pagelen">Number of rows to return.</param>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetAll(TransactionManager transactionManager, int start, int pagelen)
	{
		if (transactionManager.IsOpen)
			return GetAll(transactionManager, null, start, pagelen);
		else
			return GetAll(null, transactionManager.ConnectionString, start, pagelen);
	}//end getall
	
	/// <summary>
	/// 	Gets All rows from the DataSource.
	/// </summary>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pagelen">Number of rows to return.</param>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	protected PermissionCollection GetAll(TransactionManager transactionManager, string connectionString, int start, int pagelen)
	{
		//Declare Varibles
		SqlDataReader reader;
		if (transactionManager != null)
			reader = SqlHelper.ExecuteReader(transactionManager.TransactionObject, "prc_Permission_Get_List");
		else
			reader = SqlHelper.ExecuteReader(connectionString, "prc_Permission_Get_List");
		//Create Collection
		PermissionCollection rows = new PermissionCollection();
		Fill(reader, rows, start, pagelen);
		reader.Close();
		return rows;
	}//end getall
	
	#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="pagelen">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 pagelen, out int count)
	{
		if(UseTransaction())
		{
			return GetPaged(this.transactionManager, start, pagelen, out count);
		}
		else
		{
			return GetPaged(this.connectionString, start, pagelen, 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="pagelen">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 pagelen, out int count)
	{
		if(UseTransaction())
		{
			return GetPaged(this.transactionManager, whereClause, orderBy, start, pagelen, out count);
		}
		else
		{
			return GetPaged(this.connectionString, whereClause, orderBy, start, pagelen, 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="pagelen">Number of rows to return.</param>
	/// <param name="count">Number of rows in the DataSource.</param>
	/// <param name="connectionString">The connection string to the datasource</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetPaged(string connectionString, int start, int pagelen, out int count)
	{
		return GetPaged(null, connectionString, null, null, start, pagelen, out count);
	}

	/// <summary>
	/// Gets a page of rows from the DataSource.
	/// </summary>
	/// <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>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pagelen">Number of rows to return.</param>
	/// <param name="count">Number of rows in the DataSource.</param>
	/// <param name="connectionString">The connection string to the datasource</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetPaged(string connectionString, string whereClause, string orderBy, int start, int pagelen, out int count)
	{
		return GetPaged(null, connectionString, whereClause, orderBy, start, pagelen, 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="pagelen">Number of rows to return.</param>
	/// <param name="count">Number of rows in the DataSource.</param>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetPaged(TransactionManager transactionManager, int start, int pagelen, out int count)
	{
		if (transactionManager.IsOpen)
			return GetPaged(transactionManager, null, null, null, start, pagelen, out count);
		else
			return GetPaged(null, transactionManager.ConnectionString, null, null, start, pagelen, out count);
	}
	
		/// <summary>
	/// Gets a page of rows from the DataSource.
	/// </summary>
	/// <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>
	/// <param name="start">Row number at which to start reading.</param>
	/// <param name="pagelen">Number of rows to return.</param>
	/// <param name="count">Number of rows in the DataSource.</param>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetPaged(TransactionManager transactionManager, string whereClause, string orderBy, int start, int pagelen, out int count)
	{
		if (transactionManager.IsOpen)
			return GetPaged(transactionManager, null, whereClause, orderBy, start, pagelen, out count);
		else
			return GetPaged(null, transactionManager.ConnectionString, whereClause, orderBy, start, pagelen, 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="pagelen">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>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	protected PermissionCollection GetPaged(TransactionManager transactionManager, string connectionString, string whereClause, string orderBy, int start, int pagelen, out int count)
	{
		
		//Declare Varibles
		SqlDataReader reader;
		if (transactionManager != null)
			reader = SqlHelper.ExecuteReader(transactionManager.TransactionObject, "prc_Permission_GetPaged", whereClause, orderBy, start, pagelen);
		else
			reader = SqlHelper.ExecuteReader(connectionString, "prc_Permission_GetPaged", whereClause, orderBy, start, pagelen);
		
		reader.Read();
		count = reader.GetInt32(0);
		reader.NextResult();

		//Create Collection
		PermissionCollection rows = new PermissionCollection();
		Fill(reader, rows, 0, int.MaxValue);
		reader.Close();
		return rows;
	}
	
	#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)
	{
		if(UseTransaction())
		{
			return GetByID(this.transactionManager, ID, 0, int.MaxValue);
		}
		else
		{
			return GetByID(this.connectionString, 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="pagelen">Number of rows to return.</param>
	/// <param name="ID"></param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetByID(System.Int32 ID, int start, int pagelen)
	{
		if(UseTransaction())
		{
			return GetByID(this.transactionManager, ID, start, pagelen);
		}
		else
		{
			return GetByID(this.connectionString, ID, start, pagelen);
		}
	}

	
	/// <summary>
	/// 	Gets rows from the datasource based on the PK_Permission index.
	/// </summary>
	/// <param name="ID"></param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetByID(string connectionString, System.Int32 ID)
	{
		return GetByID(null, connectionString, 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="pagelen">Number of rows to return.</param>
	/// <param name="ID"></param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetByID(string connectionString, System.Int32 ID, int start, int pagelen)
	{
		return GetByID(null, connectionString, ID, start, pagelen);
	}

	
	/// <summary>
	/// 	Gets rows from the datasource based on the PK_Permission index.
	/// </summary>
	/// <param name="ID"></param>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetByID(TransactionManager transactionManager, System.Int32 ID)
	{
		if (transactionManager.IsOpen)
			return GetByID(transactionManager, null, ID, 0, int.MaxValue);
		else
			return GetByID(null, transactionManager.ConnectionString, 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="pagelen">Number of rows to return.</param>
	/// <param name="ID"></param>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	public PermissionCollection GetByID(TransactionManager transactionManager, System.Int32 ID, int start, int pagelen)
	{
		if (transactionManager.IsOpen)
			return GetByID(transactionManager, null, ID, start, pagelen);
		else
			return GetByID(null, transactionManager.ConnectionString, ID, start, pagelen);
	}
	
	
	/// <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="pagelen">Number of rows to return.</param>
	/// <param name="ID"></param>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <remarks></remarks>
	/// <returns>Returns a typed collection of Permission objects.</returns>
	protected PermissionCollection GetByID(TransactionManager transactionManager, string connectionString, System.Int32 ID, int start, int pagelen)
	{
		//Declare Varibles
		SqlDataReader reader;
		if (transactionManager != null)
			reader = SqlHelper.ExecuteReader(transactionManager.TransactionObject, "prc_Permission_GetByID", ID);
		else
			reader = SqlHelper.ExecuteReader(connectionString, "prc_Permission_GetByID", ID);
		//Create collection and fill
		PermissionCollection rows = new PermissionCollection();
		Fill(reader, rows, start, pagelen);
		reader.Close();
		return rows;
	}
	

	#endregion "Get By Index Functions"

	#region "Insert Functions"

	/// <summary>
	/// 	Inserts a Permission object into the datasource.
	/// </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)
	{
		if(UseTransaction())
		{
			return Insert(this.transactionManager, entity);
		}
		else
		{
			return Insert(this.connectionString, entity);
		}
	}
	
	/// <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)
	{
			if(UseTransaction())
		{
			return Insert(this.transactionManager, entityCollection);
		}
		else
		{
			return Insert(this.connectionString, entityCollection);
		}		
	}

		
	/// <summary>
	/// 	Insert rows in the datasource.
	/// </summary>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <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(string connectionString, PermissionCollection entityCollection)
	{
		int number = 0;
		foreach (Permission entity in entityCollection)
		{
			if (entity.IsNew)
			{
				if (Insert(connectionString, entity) )
				{
					number++;
				}
			}
		}
		return number;
	}
	
	/// <summary>
	/// 	Insert rows in the datasource.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <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(TransactionManager transactionManager, PermissionCollection entityCollection)
	{
		int number = 0;
		foreach (Permission entity in entityCollection)
		{
			if (entity.IsNew)
			{
				if (Insert(transactionManager, entity) )
				{
					number++;
				}
			}
		}
		return number;
	}
	
	/// <summary>
	/// 	Inserts a Permission object into the datasource.
	/// </summary>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <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(string connectionString, Permission entity)
	{
		return Insert(null, connectionString, entity);
	}
	
	
	/// <summary>
	/// 	Inserts a Permission object into the datasource using a transaction.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <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(TransactionManager transactionManager,  Permission entity)
	{
		if (transactionManager.IsOpen)
			return Insert(transactionManager, null, entity);
		else
			return Insert(null, transactionManager.ConnectionString, entity);
	}
	
	
	/// <summary>
	/// 	Inserts a Permission object into the datasource using a transaction.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <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>
	protected bool Insert(TransactionManager transactionManager, string connectionString, Permission entity)
	{
		//Declare variables			
		//IDataParameterCollection parameters;
		SqlDataReader reader;
		int result = 0;
		//Get Parameters
		//if (transactionManager != null)
		//	parameters = SqlHelperParameterCache.GetSpParameterSet(transactionManager.ConnectionString, "prc_Permission_Insert");
		//else
		//	parameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, "prc_Permission_Insert");
		//Assign values to parameters
		//AssignInsertParameters(parameters, entity);
		
		
		if (transactionManager != null)
			reader = SqlHelper.ExecuteReader(transactionManager.TransactionObject, "prc_Permission_Insert", 
			entity.ChangeStamp,entity.Description,entity.ID,entity.Title);
		else
			reader = SqlHelper.ExecuteReader(connectionString, "prc_Permission_Insert", 
			entity.ChangeStamp,entity.Description,entity.ID,entity.Title );
			
		Debug.WriteLine("PermissionRepository-Insert Affected " + reader.RecordsAffected + " records.");
		if (reader.RecordsAffected > 0)
		{
			//RefreshEntity closes the connection
			RefreshEntity(reader, entity);
			result = 1;
		}
		else
		{
			//must always close the connection
			reader.Close();
		}
		return Convert.ToBoolean(result);
	}
	
	
	
	
	/// <summary>
	/// Assigns the insert parameters from the Permission instance to the SqlParameter array.
	/// </summary>
	/// <param name="parameters">The <see cref="SqlParameter"/> array to fill.</param>
	/// <param name="entity">The <see cref="Permission"/> instance to read from.</param>
	/* protected void AssignInsertParameters(IDataParameterCollection paramCollection, Permission entity)
	{
		SqlParameter[] parameters = new SqlParameter[ paramCollection.Count ];
		paramCollection.CopyTo( parameters, 0 );
		
					parameters[0].Value = entity.ChangeStamp;
					parameters[1].Value = entity.Description;
					parameters[2].Value = entity.ID;
					parameters[3].Value = entity.Title;
	} */
	
	#endregion

	#region "Update Functions"
	
	
	/// <summary>
	/// 	Update an existing row in the datasource.
	/// </summary>
	/// <param name="entity">The <see cref="Permission"/> instance 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)
	{
		if(UseTransaction())
		{
			return Update(this.transactionManager, entity);
		}
		else
		{
			return Update(this.connectionString, entity);
		}		
	}		
	
	/// <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)
	{
		if(UseTransaction())
		{
			return Update(this.transactionManager, entityCollection);
		}
		else
		{
			return Update(this.connectionString, entityCollection);
		}	
	}

	
	/// <summary>
	/// 	Update existing rows in the datasource.
	/// </summary>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <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(string connectionString, PermissionCollection entityCollection)
	{	
		int number = 0;
		foreach (Permission entity in entityCollection)
		{
			if ((entity.IsDirty) && !(entity.IsNew))
			{
				if ( Update(connectionString, entity) )
				{
					number++;
				}
			}
		}
		return number;
	}
	
	
	/// <summary>
	/// 	Update existing rows in the datasource.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <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(TransactionManager transactionManager, PermissionCollection entityCollection)
	{
		int number = 0;
		foreach (Permission entity in entityCollection)
		{
			if ((entity.IsDirty) && !(entity.IsNew))
			{
				if ( Update(transactionManager, entity) )
				{
					number++;
				}
			}
		}
		return number;
	}

	/// <summary>
	/// 	Update an existing row in the datasource.
	/// </summary>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <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(string connectionString, Permission entity)
	{	
		return Update(null, connectionString, entity);
	}
	
	
	/// <summary>
	/// 	Update an existing row in the datasource.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <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(TransactionManager transactionManager, Permission entity)
	{
		if (transactionManager.IsOpen)
			return Update(transactionManager, null, entity);
		else
			return Update(null, transactionManager.ConnectionString, entity);
	}
	
	
	/// <summary>
	/// 	Update an existing row in the datasource.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <param name="connectionString">Connection string to datasource.</param>
	/// <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>
	protected bool Update(TransactionManager transactionManager, string connectionString, Permission entity)
	{
		//Declare variables
		//IDataParameterCollection parameters;
		SqlDataReader reader;
		int result = 0;
		//Get Parameters
		//if (transactionManager != null)
		//	parameters = SqlHelperParameterCache.GetSpParameterSet(transactionManager.ConnectionString, "prc_Permission_Update");
		//else
		//	parameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, "prc_Permission_Update");
		//Assign parameters values to corresponding entity values
		//AssignUpdateParameters(parameters, entity);
		
		
		//Get Reader
		if (transactionManager != null)
			reader = SqlHelper.ExecuteReader(transactionManager.TransactionObject, "prc_Permission_Update",
				entity.ID,entity.ChangeStamp,entity.Description,entity.Title,entity.OriginalID);
		else
			reader = SqlHelper.ExecuteReader(connectionString, "prc_Permission_Update", 
				entity.ID,entity.ChangeStamp,entity.Description,entity.Title,entity.OriginalID);
			
		Debug.WriteLine("PermissionRepository-Update Affected " + reader.RecordsAffected + " records.");
		if (reader.RecordsAffected > 0)
		{
			RefreshEntity(reader, entity);
			result = reader.RecordsAffected;
		}
		else
		{
			//must always close the connection
			reader.Close();
			DBConcurrencyException conflict = new DBConcurrencyException("Concurrency exception");
			conflict.ModifiedRecord = entity;
			PermissionCollection dsrecord;
			//Get record from Datasource
			if (transactionManager != null)
				dsrecord = PermissionRepository.Current.GetByID(this.transactionManager, entity.ID);
			else
				dsrecord = PermissionRepository.Current.GetByID(connectionString, entity.ID);
			if(dsrecord.Count > 0)
				conflict.DatasourceRecord = dsrecord[0];
			throw conflict;
		}
		return Convert.ToBoolean(result);
	}

	
	
	/// <summary>
	/// Assigns the update parameters from an to a <see cref="Permission"/> instance to an <see cref="SqlParameter"/> array.
	/// </summary>
	/// <param name="parameters">The <see cref="SqlParameter"/> array.</param>
	/// <param name="entity">The <see cref="Permission"/> instance.</param>
	//protected void AssignUpdateParameters(IDataParameterCollection paramCollection, Permission entity)
	//{
	//}
	
	#endregion
	
	
	#region "Save Functions"
	
	/// <summary>
	/// 	Updates, Inserts rows in the datasource.
	/// </summary>
	/// <param name="entityCollection"><c>Permission</c> objects in a <c>PermissionCollection</c> 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(PermissionCollection entityCollection)
	{
		if(UseTransaction())
		{
			Save(this.transactionManager, entityCollection);
		}
		else
		{
			Save(this.connectionString, entityCollection);
		}
	}
	

		
	
	/// <summary>
	/// 	Updates, Inserts rows in the datasource.
	/// </summary>
	/// <param name="connectionString">Connection String to Datasource.</param>
	/// <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(string connectionString, Permission entity)
	{		
		if (entity.IsDeleted)
			Delete(connectionString, entity);
		else if ((entity.IsDirty) && !(entity.IsNew))
			Update(connectionString, entity);
		else if (entity.IsNew)
			Insert(connectionString, entity);
	}



	/// <summary>
	/// 	Updates, Inserts rows in the datasource.
	/// </summary>
	/// <param name="connectionString">Connection String to Datasource.</param>
	/// <param name="entityCollection"><c>Permission</c> objects in a <c>PermissionCollection</c> 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(string connectionString, PermissionCollection entityCollection)
	{
		foreach (Permission entity in entityCollection)
		{			
			Save(connectionString, entity);
		}
	}
	
	
	
	
	/// <summary>
	/// 	Updates, Inserts rows in the datasource.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <param name="entity"><c>Permission</c> objects in a <c>PermissionCollection</c> 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(TransactionManager transactionManager, Permission entity)
	{		
		if (entity.IsDeleted)
		{
			Delete(transactionManager, entity);
		}
		if ((entity.IsDirty) && !(entity.IsNew))
		{
			Update(transactionManager, entity);
		}
		else if (entity.IsNew)
		{
			Insert(transactionManager, entity);
		}
	}
	
	
	/// <summary>
	/// 	Updates, Inserts rows in the datasource.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
	/// <param name="entityCollection"><c>Permission</c> objects in a <c>PermissionCollection</c> 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(TransactionManager transactionManager, PermissionCollection entityCollection)
	{
		foreach (Permission entity in entityCollection)
		{
			Save(transactionManager, 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="pagelen">number of row.</param>
	///<returns>A <see chref="PermissionCollection"/> object.</returns>
	protected PermissionCollection Fill(DataSet dataSet, PermissionCollection rows, int start, int pagelen)
	{
		int recordnum = 0;
		
		System.Collections.IEnumerator dataRows =  dataSet.Tables[0].Rows.GetEnumerator();
		
		while (dataRows.MoveNext() && (pagelen != 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"];
				rows.Add(c);
				pagelen -= 1;
			}
			recordnum += 1;
		}
		return rows;
	}

	
	///<summary>
	/// Fill an PermissionCollection From a DataReader.
	///</summary>
	/// <param name="reader">Datareader</param>
	/// <param name="rows">The collection to fill</param>
	/// <param name="start">Start row</param>
	/// <param name="pagelen">number of row.</param>
	///<returns>a <see cref="PermissionCollection"/></returns>
	protected PermissionCollection Fill(SqlDataReader reader, PermissionCollection rows, int start, int pagelen)
	{
		int recordnum = 0;
		while (reader.Read() && (pagelen != 0))
		{
			if(recordnum >= start)
			{
				Permission c = new Permission();
				c.ID = (Convert.IsDBNull(reader["ID"]))?(int)0:(System.Int32)reader["ID"];
				c.OriginalID = (Convert.IsDBNull(reader["ID"]))?(int)0:(System.Int32)reader["ID"];
				c.Title = (Convert.IsDBNull(reader["Title"]))?string.Empty:(System.String)reader["Title"];
				c.Description = (Convert.IsDBNull(reader["Description"]))?string.Empty:(System.String)reader["Description"];
				c.ChangeStamp = (Convert.IsDBNull(reader["ChangeStamp"]))?DateTime.MinValue:(System.DateTime)reader["ChangeStamp"];
				c.AcceptChanges();
				rows.Add(c);
				pagelen -= 1;
			}
			recordnum += 1;
		}
		return rows;
	}
	
	
	/// <summary>
	/// Refreshes the <see cref="Permission"/> object from the <see cref="SqlDataReader"/>.
	/// </summary>
	/// <param name="reader">The <see cref="SqlDataReader"/> to read from.</param>
	/// <param name="entity">The <see cref="Permission"/> object.</param>
	protected void RefreshEntity(SqlDataReader reader, Permission entity)
	{
		reader.Read();
		entity.ID = (Convert.IsDBNull(reader["ID"]))?(int)0:(System.Int32)reader["ID"];
		entity.OriginalID = (Convert.IsDBNull(reader["ID"]))?(int)0:(System.Int32)reader["ID"];
		entity.Title = (Convert.IsDBNull(reader["Title"]))?string.Empty:(System.String)reader["Title"];
		entity.Description = (Convert.IsDBNull(reader["Description"]))?string.Empty:(System.String)reader["Description"];
		entity.ChangeStamp = (Convert.IsDBNull(reader["ChangeStamp"]))?DateTime.MinValue:(System.DateTime)reader["ChangeStamp"];
		reader.Close();

		entity.AcceptChanges();
	}
	
	
	/// <summary>
	/// Indicates if a transaction is currently used.
	/// </summary>
	/// <returns></returns>
	protected bool UseTransaction()
	{
		return UseTransaction(this.transactionManager);
	}
	
	
	
	/// <summary>
	/// Indicates if a transaction is currently used.
	/// </summary>
	/// <param name="transactionManager"><see cref="TransactionManager"/> object.</param>
	/// <returns></returns>
	protected bool UseTransaction(TransactionManager transactionManager)
	{
		if (transactionManager != null)
		{
			if (transactionManager.IsOpen)
				return true;
		}
		return false;
	}
	#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