Click here to Skip to main content
15,896,730 members
Articles / Web Development / ASP.NET

ViewState Provider - an implementation using Provider Model Design Pattern

Rate me:
Please Sign up or sign in to vote.
4.83/5 (41 votes)
15 Aug 20049 min read 190.6K   2.9K   117  
Solve the ViewState problems using the industry well-known pattern - Provider Model design pattern.
using System;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;


namespace System.Web.UI 
{
	public class ViewStateConfiguration 
	{
		#region Member Vars
		private string defaultProvider;
		private Hashtable providers = new Hashtable();
		#endregion
		
		#region Properties
		
		public string DefaultProvider 
		{ 
			get { return defaultProvider; } 
		}
		
		public Hashtable Providers 
		{ 
			get { return providers; } 
		} 
		
		#endregion
		
		#region GetConfig
		public static ViewStateConfiguration GetConfig() 
		{
			return (ViewStateConfiguration) ConfigurationSettings.GetConfig("system.web/viewstate");
		} 
		#endregion
		
		#region LoadValuesFromConfigurationXml
		public void LoadValuesFromConfigurationXml(XmlNode node) 
		{
			XmlAttributeCollection attributeCollection = node.Attributes;
			
			// Read child nodes
			foreach (XmlNode child in node.ChildNodes) 
			{
				if (child.Name == "providers")
					GetProviders(child);
			}
									
			// Get the default provider
			defaultProvider = attributeCollection["defaultProvider"].Value;
			if (!providers.ContainsKey(defaultProvider))
				throw new ConfigurationException(String.Format("Unable to locate the [{0}] ViewStateProvider!", defaultProvider), node); 
		}
		#endregion
		
		#region GetProviders
		void GetProviders(XmlNode node) 
		{
			foreach (XmlNode provider in node.ChildNodes) 
			{
				switch (provider.Name) {
					case "add" :
						providers.Add(provider.Attributes["name"].Value, new Provider(provider.Attributes) );
						break;

					case "remove" :
						providers.Remove(provider.Attributes["name"].Value);
						break;

					case "clear" :
						providers.Clear();
						break;
				}
			}
		}
		#endregion
	}

	public class Provider 
	{
		#region Member Vars
		private string name;
		private string providerType;
		private NameValueCollection providerAttributes = new NameValueCollection();
		#endregion
		
		#region Properties

		public string Name 
		{
			get { return name; }
		}
		
		public string Type 
		{
			get { return providerType; }
		}
		
		public NameValueCollection Attributes 
		{
			get { return providerAttributes; }
		}
		
		#endregion
		
		#region ctor
		public Provider (XmlAttributeCollection attributes) 
		{
			// Set the name of the provider
			//
			name = attributes["name"].Value;

			// Set the type of the provider
			//
			providerType = attributes["type"].Value;

			// Store all the attributes in the attributes bucket
			foreach (XmlAttribute attribute in attributes) 
			{
				if ( (attribute.Name != "name") && (attribute.Name != "type") )
					providerAttributes.Add(attribute.Name, attribute.Value);
			}
		}
		#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
Singapore Singapore
Elvin Cheng is currently living in Woodlands, Singapore. He has been developing applications with the .NET Framework, using C# and ASP.NET since October 2002. Elvin specializes in building Real-time monitoring and tracking information system for Semi-conductor manufacturing industry. During his spare time, he enjoys reading books, watching movie and gym.

Comments and Discussions