Click here to Skip to main content
15,886,578 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.Collections;
using System.Runtime.CompilerServices;

using Yap.Data.Client.Properties;


namespace Yap.Data.Client
{
	/// <summary>
	/// Represents the scope stack for command execution.
	/// </summary>
	internal class ScopeStack
	{
		[ThreadStatic]
		private static ScopeStack _Instance;

		/// <summary>
		/// Gets the <see cref="Yap.Data.Client.ScopeStack"/> for current thread.
		/// </summary>
		public static ScopeStack Instance
		{
			[MethodImpl(MethodImplOptions.Synchronized)]
			get
			{
				if (_Instance == null)
				{
					_Instance = new ScopeStack();
				}
				return _Instance;
			}
		}

		private readonly Stack _CurrentStack;

		/// <summary>
		/// Registers specified scope making it the active one.
		/// </summary>
		/// <param name="scope">The scope to register.</param>
		public void RegisterScope(Scope scope)
		{
			_CurrentStack.Push(scope);
		}

		/// <summary>
		/// Unregisters specified scope.
		/// </summary>
		/// <remarks>Specified scope should be on top of the current thread's scope stack to be unregistered.</remarks>
		/// <param name="scope">The scope to unregister.</param>
		public void UnregisterScope(Scope scope)
		{
			if (GetActiveScope() != scope)
			{
				throw new InvalidOperationException(
					Resources.ItIsNotAllowedToUnregisterInactiveScopeException);
			}
			_CurrentStack.Pop();
		}

		/// <summary>
		/// Gets the active scope from stack.
		/// </summary>
		/// <returns></returns>
		public Scope GetActiveScope()
		{
			if (_CurrentStack.Count == 0)
			{
				return null;
			}
			return _CurrentStack.Peek() as Scope;
		}

		/// <summary>
		/// Gets the value indicating whether active scope exists.
		/// </summary>
		public Boolean HasActiveScope
		{
			get
			{
				return GetActiveScope() != null;
			}
		}

		private ScopeStack()
		{
			_CurrentStack = new Stack();
		}
	}
}

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