Click here to Skip to main content
15,895,084 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.7K   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.ComponentModel;

namespace BrainTechLLC.DAL
{
	public partial class DBLayer<T> : INotifyPropertyChanged, ISuppliesID where T : DBLayer<T>, IDBLayer<T>, new()
	{
		public static LookupCollection<T> Find
		{
			get
			{
				if (Cache == null) EnsureInitialized();
				return Cache.Find;
			}
		}

		// Supports generic retrieval of objects by unique key - implements ISuppliesID interface
		public DBLayer<T2> Get<T2>(int id) where T2 : DBLayer<T2>, IDBLayer<T2>, new() { return Get(id) as DBLayer<T2>; }
		public object GetObject(int id) { return DBLayer<T>.Find[id]; }
		public static T Get(int id) { return Find[id]; }

		public DBLayer<T2> Get<T2>(long id) where T2 : DBLayer<T2>, IDBLayer<T2>, new() { return Get(id) as DBLayer<T2>; }
		public long GetID() { PropertyDescriptor pd = CachedAttributes.QuickLookupProperty; return (pd == null) ? 0 : Convert.ToInt64(pd.GetValue(this)); }
		public object GetObject(long id) { return DBLayer<T>.Find[id]; }
		public static T Get(long id) { return Find[id]; }

		// Note - below method will throw an unhandled exception if the friendly object name property has a null value
		public string FriendlyObjectName { get { PropertyDescriptor pd = CachedAttributes.ObjectFriendlyNameProperty; return (pd == null) ? "" : Convert.ToString(pd.GetValue(this)); } }
	}
}

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