Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » General » Downloads
 
Add your own
alternative version

Data Access and Transaction Handling Framework

, 13 Jan 2005
Generic data access component for different datasources, sprocs/SQL, implicitly propagated transactions, explicitly managed transaction contexts etc.
dac2.zip
Framework
Configuration
bin
Exceptions
DataAccess
bin
DataAccessSettings.xsx
DataOperations.xsx
Exceptions
ESTransactionHandler
bin
Framework.snk
TransactionHandling
bin
Transactions
bin
Exceptions
TransactionHandlingSettings.xsx
UnitTests
bin
config
bin
Framework.Configuration.dll
Framework.DataAccess.dll
Framework.DataAccess.TransactionHandling.dll
Framework.DataAccess.TransactionHandling.ESTransactionHandler.dll
Framework.Transactions.dll
FrameworkSampleApp
SampleBLL
bin
SampleConsoleClient
App.ico
bin
config
SampleDAL
bin
SampleFacade
bin
SampleState
bin
SampleWebClient
bin
config
Global.asax
SampleWebClient.csproj.webinfo
using System;
using System.Data;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Collections;
using System.Configuration;

namespace Framework.DataAccess
{
	/// <summary>
	/// DataProviderFactory returns DataProvider objects.
	/// Maintains/refreshes internal cache of DataProviders.
	/// Config info retrieved from Framework.DataAccess.dll.config
	/// </summary>
	internal class DataProviderFactory
	{
		private static Hashtable _dataProviders = null;

		static DataProviderFactory()
		{
			LoadCache();
		}

		private static void LoadCache()
		{
			try
			{
				dataAccessSettings settings = 
					(dataAccessSettings)ConfigurationSettings.GetConfig("dataAccessSettings");

				_dataProviders = new Hashtable();

				foreach(dataProvider dp in settings.dataProviders)
				{
					Type connectionType = Type.GetType(dp.connectionType);
					if(connectionType == null)
						throw new DataAccessException("Could not load connectionType for typeName " + dp.connectionType + " for provider " + dp.name);

					Type commandType = Type.GetType(dp.commandType);
					if(commandType == null)
						throw new DataAccessException("Could not load commandType for typeName " + dp.commandType + " for provider " + dp.name);

					Type parameterType = Type.GetType(dp.parameterType);
					if(parameterType == null)
						throw new DataAccessException("Could not load parameterType for typeName " + dp.parameterType + " for provider " + dp.name);

					PropertyInfo parameterDbTypeProperty = 
						parameterType.GetProperty(
						dp.parameterDbTypeProperty, 
						BindingFlags.Instance | BindingFlags.Public);
					if(parameterDbTypeProperty == null)
						throw new DataAccessException("Could not load parameterDbTypeProperty for typeName " + dp.parameterDbTypeProperty + " for provider " + dp.name);

					Type parameterDbType = Type.GetType(dp.parameterDbType);
					if(parameterDbType == null)
						throw new DataAccessException("Could not load parameterDbType for typeName " + dp.parameterDbType + " for provider " + dp.name);

					Type dataAdapterType = null;
					if(dp.dataAdapterType != null && dp.dataAdapterType != string.Empty) 
					{
						dataAdapterType = Type.GetType(dp.dataAdapterType);
						if(dataAdapterType == null)
							throw new DataAccessException("Could not load dataAdapterType for typeName " + dp.dataAdapterType + " for provider " + dp.name);
					}

					Type commandBuilderType = null;
					if(dp.commandBuilderType != null && dp.commandBuilderType != string.Empty) 
					{
						commandBuilderType = Type.GetType(dp.commandBuilderType);
						if(commandBuilderType == null)
							throw new DataAccessException("Could not load commandBuilderType for typeName " + dp.commandBuilderType + " for provider " + dp.name);
					}
								
					if(dp.parameterNamePrefix == null)
						dp.parameterNamePrefix = string.Empty;

					_dataProviders.Add(dp.name, 
						new DataProvider(
						dp.name, connectionType, commandType, 
						parameterType, parameterDbType, parameterDbTypeProperty,
						dataAdapterType, commandBuilderType, dp.parameterNamePrefix));
				
				}
			}
			catch(Exception e)
			{
				throw new DataAccessException("Error loading cache from config.", e);
			}
		}

		//only static methods
		private DataProviderFactory() {}

		internal static DataProvider GetDataProvider(string dataProviderName)
		{
			DataProvider dp = _dataProviders[dataProviderName] as DataProvider;

			if(dp == null) 
			{
				//TODO: specialize the exception, perhaps DataSourceNotFoundException
				throw new DataAccessException("DataProvider with name '" + dataProviderName + "' not found.");
			}

			return dp;
		}

		#region OLD 
//		private static void LoadDataProviders(string xml) 
//		{
//			XmlTextReader xr = new XmlTextReader(new StringReader(xml));
//
//			xr.WhitespaceHandling = WhitespaceHandling.None;
//			xr.MoveToContent();
//
//			Hashtable dataProviders = new Hashtable();
//			string name = "";
//
//			while(xr.Read())
//			{
//				switch(xr.NodeType) 
//				{
//					case XmlNodeType.Element:
//						switch(xr.Name) 
//						{
//
//						}
//					break;
//				}
//			}
//			xr.Close();
//
//			_dataProviders = dataProviders;
//		}
		#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 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

Share

About the Author

Deyan Petrov
Web Developer
Austria Austria
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 13 Jan 2005
Article Copyright 2003 by Deyan Petrov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid