Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C#

Data Access and Transaction Handling Framework

Rate me:
Please Sign up or sign in to vote.
4.69/5 (107 votes)
13 Jan 200510 min read 916.8K   2.1K   282  
Generic data access component for different datasources, sprocs/SQL, implicitly propagated transactions, explicitly managed transaction contexts etc.
using System;
using System.Configuration;
using System.Collections;
using System.Xml;
using System.IO;

namespace Framework.Transactions
{
	// A delegate type for hooking up transaction context state notifications.
	public delegate void TCCreatedEventHandler(object sender, TCCreatedEventArgs e);

	/// <summary>
	/// A customized EventArgs holding the created transaction context
	/// </summary>
	public class TCCreatedEventArgs : EventArgs 
	{
		public TransactionContext Context;

		public TCCreatedEventArgs(TransactionContext context) 
		{
			this.Context = context;
		}
	}

	/// <summary>
	/// TransactionContextFactory creates different transaction contexts 
	/// and raises a ContextCreated event.
	/// Additionally initializes the transaction handler factory. 
	/// Config info retrieved from Framework.Transactions.dll.config
	/// </summary>
	public class TransactionContextFactory
	{
		private TransactionContextFactory() {}

		private static ITransactionHandler _th = null;

		public static ITransactionHandler GetHandler()
		{
			return _th;
		}

		static TransactionContextFactory()
		{
			//load 
			try
			{
				transactionHandlingSettings settings = 
					(transactionHandlingSettings)ConfigurationSettings.GetConfig("transactionHandlingSettings");

				if(settings == null) return;

				Type handlerType = Type.GetType(settings.transactionHandler.handlerType);
				if(handlerType == null)
					throw new ApplicationException("Could not load handlerType for typeName " + settings.transactionHandler.handlerType + " for transactionHandler " + settings.transactionHandler.name);
				_th = (ITransactionHandler)Activator.CreateInstance(handlerType);

				ContextCreated += new TCCreatedEventHandler(_th.HandleTCCreated);
			}
			catch(Exception e)
			{
				throw new TransactionHandlingException("Error loading transactionHandler from config.", e);
			}
		}

		// An event that clients can use to be notified whenever the
		// the a new transaction context is created.
		public static event TCCreatedEventHandler ContextCreated;

		public static TransactionContext GetContext(TransactionAffinity transactionAffinity) 
		{
			TransactionContext ctx = null;

			switch(transactionAffinity)
			{
				case TransactionAffinity.RequiresNew:
					ctx = new RequiresNewTransactionContext();
					break;
				case TransactionAffinity.Required:
					ctx = new RequiredTransactionContext();
					break;
				case TransactionAffinity.Supported:
					ctx = new SupportedTransactionContext();
					break;
				case TransactionAffinity.NotSupported:
					ctx = new NotSupportedTransactionContext();
					break;
				default:
					throw new TransactionContextException(transactionAffinity.ToString() + "is not currently supported.");
			}

			if(ContextCreated != null)
				ContextCreated(null, new TCCreatedEventArgs(ctx));

			return ctx;
		}

		public static TransactionContext GetCurrentContext() 
		{
			return TransactionContext.Current;
		}

		public static TransactionContext EnterContext(TransactionAffinity transactionAffinity) 
		{
			TransactionContext ctx = GetContext(transactionAffinity);
			ctx.Enter();
			return ctx;
		}
	}
}

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
Web Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions