Click here to Skip to main content
15,885,278 members
Articles / Database Development / SQL Server / SQL Server 2008

Simple Data Access in C#

Rate me:
Please Sign up or sign in to vote.
4.11/5 (15 votes)
11 Jan 2009CPOL5 min read 83.7K   1.4K   58  
Fast and easy to use data access class library.
using System;
using System.Data;
using System.Configuration;


namespace Yap.Data.Client
{
	/// <summary>
	/// Represents an scope of <see cref="Yap.Data.Client.Command"/> execution in the current thread.
	/// This is the abstract base class for <see cref="Yap.Data.Client.SessionScope"/> and <see cref="Yap.Data.Client.TransactionScope"/>.
	/// </summary>
	public abstract class Scope : IDisposable
	{
		private static readonly Object _ConfigSync = new Object();
		private static IConfiguration _Configuration;

		/// <summary>
		/// Gets or sets default Yap.Data.Client configuration.
		/// </summary>
		public static IConfiguration Configuration
		{
			get
			{
				lock (_ConfigSync)
				{
					if (_Configuration == null)
					{
						_Configuration = ConfigurationManager.GetSection("dataAccess")
							as ConfigurationSectionHandler;
					}
					return _Configuration;
				}
			}
			set
			{
				lock (_ConfigSync)
				{
					if (value == null)
					{
						throw new ArgumentNullException("value");
					}
					_Configuration = value;
				}
			}
		}

		/// <summary>
		/// Gets the value indicating whether active scope exists in current thread.
		/// </summary>
		/// <returns></returns>
		public static Boolean IsActiveScopeExist()
		{
			return ScopeStack.Instance.HasActiveScope;
		}

		/// <summary>
		/// Gets the active scope in current thread.
		/// </summary>
		public static Scope Active
		{
			get
			{
				return ScopeStack.Instance.GetActiveScope();
			}
		}

		private readonly Scope _ParentScope;

		internal Scope ParentScope
		{
			get { return _ParentScope; }
		}

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		protected Scope()
		{
			_ParentScope = ScopeStack.Instance.GetActiveScope();
			ScopeStack.Instance.RegisterScope(this);
		}

		/// <summary>
		/// Releases all resources used by the current instance.
		/// </summary>
		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		private void Dispose(Boolean disposing)
		{
			if (disposing)
			{
				ScopeStack.Instance.UnregisterScope(this);
			}
			PerformDisposal(disposing);
		}

		/// <summary>
		/// Frees resources and performs other cleanup operations before the instance is reclaimed by garbage collection.
		/// </summary>
		~Scope()
		{
			Dispose(false);
		}

		/// <summary>
		/// Gets existing <see cref="System.Data.IDbConnection"/> for current thread.
		/// </summary>
		abstract public IDbConnection Connection{ get; }

		/// <summary>
		/// Gets existing <see cref="System.Data.IDbTransaction"/> for current thread.
		/// </summary>
		abstract public IDbTransaction Transaction { get; }

		/// <summary>
		/// Releases the managed resources used by the current instance and optionally releases the managed resources.
		/// </summary>
		/// <param name="disposing">true to release managed and unmanaged resources; false to release only unmanaged resources.</param>
		protected abstract void PerformDisposal(Boolean disposing);
	}
}

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 Code Project Open License (CPOL)


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

Comments and Discussions