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

Design and Implementation of an Attribute-Driven, Caching Data Abstraction Layer

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
21 Jul 2008CPOL30 min read 68.4K   595   103  
An easy-to-use, attribute-driven data abstraction layer with multi-database support, intelligent caching, transparent encryption, multi-property sorting, property change tracking, etc.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Globalization;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC.DAL
{
	public partial class DBLayer<T> where T : DBLayer<T>, IDBLayer<T>, new()
	{
		/// <summary>
		/// Stores generated (integer) indices on the fly.  Each dictionary entry stores a property value name.  Associated
		/// with the property value name: another dictionary which maps Integer values to "rows" where property value = Integer value
		/// </summary>
		internal static Dictionary<string, Dictionary<long, ResultSet<T>>> _integerIndices = new Dictionary<string, Dictionary<long, ResultSet<T>>>(32, StringComparer.OrdinalIgnoreCase);

		/// <summary>
		/// Stores generated (string) indices on the fly.  (See above)
		/// </summary>
		internal static Dictionary<string, Dictionary<string, ResultSet<T>>> _stringIndices = new Dictionary<string, Dictionary<string, ResultSet<T>>>(32, StringComparer.OrdinalIgnoreCase);

		internal static void CheckDynamicIntIndices(PropertyNameAndValueCollection pnv, int propNameValueIndex, long numericValue, out int itemCount, out bool createdIndex, ref ResultSet<T> results)
		{
			createdIndex = false;
			Dictionary<long, ResultSet<T>> dynamicIndex;
			if (_integerIndices.TryGetValue(pnv._propertyNameAndValuePairs[propNameValueIndex]._propertyName, out dynamicIndex) == true)
			{
				ResultSet<T> indexedResults;

				if (WhereCache.CheckCacheDirty())
				{
					results.InvalidateCachedProperties();
					_integerIndices.Clear();
					_stringIndices.Clear();
					itemCount = results.RowCount;
				}
				else
				{
					// For the value we are intered in (numericValue), see if we have already stored the
					// results for a search on this value.  If so, start with this shortened result set
					// (fewer entries / rows to search)
					if (dynamicIndex.TryGetValue(numericValue, out indexedResults) == true)
					{
						itemCount = indexedResults.RowCount;
						results = indexedResults;
					}
					else
					{
						createdIndex = true;
						itemCount = results.RowCount;
					}
				}

			}
			else // no dynamic index found, but one can be made
			{
				createdIndex = true;
				itemCount = results.RowCount;
			}
		}

		internal static void CheckDynamicStringIndices(PropertyNameAndValueCollection pnv, int propNameValueIndex, string stringValue, out int itemCount, out bool createdIndex, ref ResultSet<T> results)
		{
			createdIndex = false;
			Dictionary<string, ResultSet<T>> dynamicStringIndex;

			// Handle/look for string indices
			if (_stringIndices.TryGetValue(pnv._propertyNameAndValuePairs[propNameValueIndex]._propertyName, out dynamicStringIndex) == true)
			{
				ResultSet<T> indexedResults;
				if (WhereCache.CheckCacheDirty())
				{
					results.InvalidateCachedProperties(); _integerIndices.Clear(); _stringIndices.Clear();
					itemCount = results.RowCount;
				}
				else
				{
					if (dynamicStringIndex.TryGetValue(stringValue, out indexedResults) == true)
					{
						itemCount = indexedResults.RowCount;
						results = indexedResults;
					}
					else
					{
						createdIndex = true;
						itemCount = results.RowCount;
					}
				}
			}
			else // no dynamic index found, but one can be made
			{
				createdIndex = true;
				itemCount = results.RowCount;
			}
		}		
	}
}

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) Troppus Software
United States United States
Currently working as a Senior Silverlight Developer with Troppus Software in Superior, CO. I enjoy statistics, programming, new technology, playing the cello, and reading codeproject articles. Smile | :)

Comments and Discussions