Click here to Skip to main content
15,884,047 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.5K   2.1K   282  
Generic data access component for different datasources, sprocs/SQL, implicitly propagated transactions, explicitly managed transaction contexts etc.
using System;
using System.IO;
using System.Reflection;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;

namespace Framework.Configuration
{
	/// <summary>
	/// Summary description for ValidatingXmlConfigSectionHandler.
	/// </summary>
	public class ValidatingXmlConfigSectionHandler : IConfigurationSectionHandler
	{
		private Type _configType = null;
		private string _schemaResourceName = string.Empty;
		private string _schemaNamespace = string.Empty;

		public ValidatingXmlConfigSectionHandler(Type configType, 
			string schemaResourceName,  string schemaNamespace)
		{
			if(configType == null)
				throw new ArgumentException("configType should be specified.");
			if(schemaResourceName == null || schemaResourceName == string.Empty)
				throw new ArgumentException("schemaResourceName should be specified.");
			if(schemaNamespace == null || schemaNamespace == string.Empty)
				throw new ArgumentException("schemaNamespace should be specified.");

			_configType = configType;
			_schemaResourceName = schemaResourceName;
			_schemaNamespace = schemaNamespace;
		}

		#region IConfigurationSectionHandler Members

		public object Create(object parent, object configContext, System.Xml.XmlNode section)
		{
			object settings = null;

			XmlSerializer ser = new XmlSerializer(_configType);

			XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader(
				new StringReader(section.OuterXml)));
			vr.ValidationType = ValidationType.Schema;
		
			Stream schemaStream = Assembly.GetAssembly(_configType)
				.GetManifestResourceStream(_schemaResourceName);

			XmlTextReader sr = new XmlTextReader(schemaStream);

			vr.Schemas.Add(_schemaNamespace, sr);

			settings = ser.Deserialize(vr);

			return settings;
		}

		#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


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