Click here to Skip to main content
15,895,606 members
Articles / Programming Languages / C#

Features of the nHydrate DAL and Entity Framework generators: Part 1 - Auditing

,
Rate me:
Please Sign up or sign in to vote.
3.40/5 (3 votes)
22 Jul 2010Ms-PL4 min read 22.6K   97   4  
This article details how to use nHydrate to add an auditing framework to the nHydrate DAL and Entity Framework DAL.
using System;
using System.Data;
using System.Collections;
using System.Runtime.Serialization;
using Widgetsphere.Core.Util;
using Widgetsphere.Core.DataAccess;
using System.Collections.Generic;
using Acme.AuditExample.Business.Objects;
using Acme.AuditExample.Domain.Objects;
using System.Xml;

namespace Acme.AuditExample.Business.SelectCommands
{
	#region CustomerSelectAll

	/// <summary>
	/// The select command used to select all rows from the 'Customer' table
	/// </summary>
	[Serializable]
	public partial class CustomerSelectAll : SelectCommandBase, ISerializable
	{

		#region Serialization

		/// <summary>
		/// Serialization constructor
		/// </summary>
		protected CustomerSelectAll(SerializationInfo info, StreamingContext context): base(info,context)
		{
		}

		/// <summary>
		/// Method used internally for serialization
		/// </summary>
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
		}

		#endregion 

		/// <summary>
		/// Default constructor
		/// </summary>
		public CustomerSelectAll()
		{
		}

		/// <summary>
		/// Creates a persistable domainCollection
		/// </summary>
		public override IDomainCollection CreateDomainCollection()
		{
			DomainCustomerCollection colDomain = new DomainCustomerCollection();
			return colDomain;
		}

		/// <summary>
		/// The stored procedure name to select values to populate a 'Customer' collection
		/// </summary>
		public override string StoredProcedureName
		{
			get { return "[dbo].[gen_CustomerSelect]"; }
		}

		/// <summary>
		/// Initializes the parameters for this select command
		/// </summary>
		protected override void SetupParameterValues(SubDomainBase subDomain)
		{
		}

	}

	#endregion

	#region CustomerSelectByPks

	/// <summary>
	/// A select command that selects objects by primary key
	/// </summary>
	[Serializable]
	public partial class CustomerSelectByPks : SelectCommandBase, ISerializable
	{
		private List<CustomerPrimaryKey> mPrimaryKeys = null;

		#region Serialization

		/// <summary>
		/// Serialization constructor
		/// </summary>
		protected CustomerSelectByPks(SerializationInfo info, StreamingContext context): base(info,context)
		{
			mPrimaryKeys = new List<CustomerPrimaryKey>();
			mPrimaryKeys.AddRange((CustomerPrimaryKey[])info.GetValue("mPrimaryKeys", typeof(CustomerPrimaryKey[])));
		}

		/// <summary>
		/// Method used internally for serialization
		/// </summary>
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			info.AddValue("mPrimaryKeys", mPrimaryKeys.ToArray());
		}

		#endregion 

		/// <summary>
		/// Select objects by primary key
		/// </summary>
		public CustomerSelectByPks(System.Guid customerid)
		{
			mPrimaryKeys = new List<CustomerPrimaryKey>();
			mPrimaryKeys.Add(new CustomerPrimaryKey(customerid));

		}

		/// <summary>
		/// Select objects by a primary key
		/// </summary>
		public CustomerSelectByPks(CustomerPrimaryKey primaryKey)
		{
			mPrimaryKeys = new List<CustomerPrimaryKey>();
			mPrimaryKeys.Add(primaryKey);
		}

		/// <summary>
		/// Select objects by a list of primary keys
		/// </summary>
		public CustomerSelectByPks(IEnumerable<CustomerPrimaryKey> primaryKeys)
		{
			mPrimaryKeys = new List<CustomerPrimaryKey>();
			mPrimaryKeys.AddRange(primaryKeys);
		}

		/// <summary>
		/// Creates a persistable domainCollection
		/// </summary>
		public override IDomainCollection CreateDomainCollection()
		{
			DomainCustomerCollection colDomain = new DomainCustomerCollection();
			return colDomain;
		}

		/// <summary>
		/// The stored procedure name to select values by primary key to populate a 'Customer' collection
		/// </summary>
		public override string StoredProcedureName
		{
			get
			{
				if (mPrimaryKeys.Count == 1) return "[dbo].[gen_CustomerSelectByCustomerSinglePk]";
				else return "[dbo].[gen_CustomerSelectByCustomerPks]";
			}
		}

		/// <summary>
		/// Initializes the parameters for this select command
		/// </summary>
		protected override void SetupParameterValues(SubDomainBase subDomain)
		{
			if (mPrimaryKeys.Count == 1)
			{
				CustomerPrimaryKey key = ((CustomerPrimaryKey)mPrimaryKeys[0]);
				ParameterValues.Add("@CustomerId", key.CustomerId);
			}
			else
				ParameterValues.Add("@xml", DomainCustomerCollection.GetPrimaryKeyXml(mPrimaryKeys));
		}

	}

	#endregion

	#region CustomerSelectByCreatedDateRange

	/// <summary>
	/// Select objects by their created date.
	/// </summary>
	[Serializable]
	public partial class CustomerSelectByCreatedDateRange : SelectCommandBase, ISerializable
	{
		private DateTime? mStartDate;
		private DateTime? mEndDate;

		#region Serialization

		/// <summary>
		/// Select objects by their created date.
		/// </summary>
		protected CustomerSelectByCreatedDateRange(SerializationInfo info, StreamingContext context): base(info,context)
		{
			mStartDate = info.GetDateTime("mStartDate");
			mEndDate = info.GetDateTime("mEndDate");
		}

		/// <summary>
		/// Method used internally for serialization
		/// </summary>
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			info.AddValue("mStartDate", mStartDate);
			info.AddValue("mEndDate", mEndDate);
		}

		#endregion 

		/// <summary>
		/// Select objects by their created date.
		/// </summary>
		public CustomerSelectByCreatedDateRange(DateTime? startDate, DateTime? endDate)
		{
			mStartDate = startDate;
			mEndDate = endDate;
		}

		/// <summary>
		/// Creates a persistable domainCollection
		/// </summary>
		public override IDomainCollection CreateDomainCollection()
		{
			DomainCustomerCollection colDomain = new DomainCustomerCollection();
			return colDomain;
		}

		/// <summary>
		/// The stored procedure name to select values to populate a 'Customer' collection in a paged fashion
		/// </summary>
		public override string StoredProcedureName
		{
			get { return "[dbo].[gen_CustomerSelectByCreatedDateRange]"; }
		}

		/// <summary>
		/// Initializes the parameters for this select command
		/// </summary>
		protected override void SetupParameterValues(SubDomainBase subDomain)
		{
			ParameterValues.Add("@start_date", mStartDate);
			ParameterValues.Add("@end_date", mEndDate);
		}

		/// <summary>
		/// Execute this select command on the database
		/// </summary>
		public override IDomainCollection Execute(SubDomainBase subDomain)
		{
			this.SetupParameterValues(subDomain);
			PersistableDomainCollectionBase returnTable = (PersistableDomainCollectionBase)this.CreateDomainCollection();

			IDbCommand fillCommand = PersistableDomainCollectionBase.GetFillCommand(this.ConnectionString, this.StoredProcedureName);
			fillCommand.CommandTimeout = this.DefaultTimeOut;
			PersistableDomainCollectionBase.SetParameterValue(fillCommand, "@start_date", mStartDate);
			PersistableDomainCollectionBase.SetParameterValue(fillCommand, "@end_date", mEndDate);
			returnTable.FillDataTable(this.ConnectionString, fillCommand);
			return returnTable;
		}

	}

	#endregion

	#region CustomerSelectByModifiedDateRange

	/// <summary>
	/// Select objects by their modified date.
	/// </summary>
	[Serializable]
	public partial class CustomerSelectByModifiedDateRange : SelectCommandBase, ISerializable
	{
		private DateTime? mStartDate;
		private DateTime? mEndDate;

		#region Serialization

		/// <summary>
		/// Select objects by their modified date.
		/// </summary>
		protected CustomerSelectByModifiedDateRange(SerializationInfo info, StreamingContext context): base(info,context)
		{
			mStartDate = info.GetDateTime("mStartDate");
			mEndDate = info.GetDateTime("mEndDate");
		}

		/// <summary>
		/// Method used internally for serialization
		/// </summary>
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			info.AddValue("mStartDate", mStartDate);
			info.AddValue("mEndDate", mEndDate);
		}

		#endregion 

		/// <summary>
		/// Select objects by their modified date.
		/// </summary>
		public CustomerSelectByModifiedDateRange(DateTime? startDate, DateTime? endDate)
		{
			mStartDate = startDate;
			mEndDate = endDate;
		}

		/// <summary>
		/// Creates a persistable domainCollection
		/// </summary>
		public override IDomainCollection CreateDomainCollection()
		{
			DomainCustomerCollection colDomain = new DomainCustomerCollection();
			return colDomain;
		}

		/// <summary>
		/// The stored procedure name to select values to populate a 'Customer' collection in a paged fashion
		/// </summary>
		public override string StoredProcedureName
		{
			get { return "[dbo].[gen_CustomerSelectByModifiedDateRange]"; }
		}

		/// <summary>
		/// Initializes the parameters for this select command
		/// </summary>
		protected override void SetupParameterValues(SubDomainBase subDomain)
		{
			ParameterValues.Add("@start_date", mStartDate);
			ParameterValues.Add("@end_date", mEndDate);
		}

		/// <summary>
		/// Execute this select command on the database
		/// </summary>
		public override IDomainCollection Execute(SubDomainBase subDomain)
		{
			this.SetupParameterValues(subDomain);
			PersistableDomainCollectionBase returnTable = (PersistableDomainCollectionBase)this.CreateDomainCollection();

			IDbCommand fillCommand = PersistableDomainCollectionBase.GetFillCommand(this.ConnectionString, this.StoredProcedureName);
			fillCommand.CommandTimeout = this.DefaultTimeOut;
			PersistableDomainCollectionBase.SetParameterValue(fillCommand, "@start_date", mStartDate);
			PersistableDomainCollectionBase.SetParameterValue(fillCommand, "@end_date", mEndDate);
			returnTable.FillDataTable(this.ConnectionString, fillCommand);
			return returnTable;
		}

	}

	#endregion

}

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, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)



Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions