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

Web User Forms for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.90/5 (52 votes)
18 Sep 2012CPOL12 min read 135.3K   4.5K   187  
User driven runtime dynamic ASP.NET Web Forms
// -- FILE ------------------------------------------------------------------
// name       : LookupRepository.cs
// project    : Itenso Web User Forms
// created    : Jani Giannoudis - 2008.10.30
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2008-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.Collections.Generic;

namespace Itenso.WebUserForms.Data.Lookup
{

	// ------------------------------------------------------------------------
	public abstract class LookupRepository : ILookupRepository
	{

		// ----------------------------------------------------------------------
		protected LookupRepository()
		{
		} // LookupRepository

		// ----------------------------------------------------------------------
		public ILookupValueCollection this[ string lookupName ]
		{
			get { return GetLookup( lookupName ); }
		} // this[string]

		// ----------------------------------------------------------------------
		public ILookupValueCollection GetLookup( string lookupName )
		{
			return GetLookup( lookupName, null );
		} // GetLookup

		// ----------------------------------------------------------------------
		public ILookupValueCollection GetLookup( string lookupName, string formType )
		{
			if ( string.IsNullOrEmpty( lookupName ) )
			{
				throw new ArgumentException( "lookupName" );
			}

			string lookupKey = GetLookupKey( lookupName, formType );
			if ( this.lookups.ContainsKey( lookupKey ) )
			{
				return this.lookups[ lookupKey ];
			}

			ILookupValueCollection lookupValues = OnLookupRequest( lookupName, formType );
			if ( lookupValues == null )
			{
				return null;
			}

			this.lookups.Add( lookupKey, lookupValues );
			return lookupValues;
		} // GetLookup

		// ----------------------------------------------------------------------
		public bool Contains( string lookupName )
		{
			return Contains( lookupName, null );
		} // Contains

		// ----------------------------------------------------------------------
		public bool Contains( string lookupName, string formType )
		{
			if ( string.IsNullOrEmpty( lookupName ) )
			{
				throw new ArgumentException( "lookupName" );
			}

			return this.lookups.ContainsKey( GetLookupKey( lookupName, formType ) );
		} // Contains

		// ----------------------------------------------------------------------
		public void Add( string lookupName, ILookupValueCollection lookupValues )
		{
			Add( lookupName, null, lookupValues );
		} // Add

		// ----------------------------------------------------------------------
		public void Add( string lookupName, string formType, ILookupValueCollection lookupValues )
		{
			if ( string.IsNullOrEmpty( lookupName ) )
			{
				throw new ArgumentException( "lookupName" );
			}

			this.lookups.Add( GetLookupKey( lookupName, formType ), lookupValues );
		} // Add

		// ----------------------------------------------------------------------
		public void Clear()
		{
			this.lookups.Clear();
		} // Clear

		// ----------------------------------------------------------------------
		public void Remove( string lookupName )
		{
			Remove( lookupName, null );
		} // Remove

		// ----------------------------------------------------------------------
		public void Remove( string lookupName, string formType )
		{
			if ( string.IsNullOrEmpty( lookupName ) )
			{
				throw new ArgumentException( "lookupName" );
			}

			string lookupKey = GetLookupKey( lookupName, formType );
			if ( !this.lookups.ContainsKey( lookupKey ) )
			{
				return;
			}

			this.lookups.Remove( lookupKey );
		} // Remove

		// ----------------------------------------------------------------------
		protected abstract ILookupValueCollection OnLookupRequest( string lookupName, string formType );

		// ----------------------------------------------------------------------
		private static string GetLookupKey( string lookupName, string formType )
		{
			if ( string.IsNullOrEmpty( formType ) )
			{
				return lookupName;
			}

			return string.Concat( lookupName, ".", formType );
		} // GetLookupKey

		// ----------------------------------------------------------------------
		// members
		private readonly Dictionary<string, ILookupValueCollection> lookups = 
			new Dictionary<string, ILookupValueCollection>();

	} // class LookupRepository

} // namespace Itenso.WebUserForms.Data.Lookup
// -- EOF -------------------------------------------------------------------

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
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions