Click here to Skip to main content
15,885,056 members
Articles / Programming Languages / SQL

Abstract Data Access Layer Design

Rate me:
Please Sign up or sign in to vote.
4.85/5 (16 votes)
5 Sep 2009CPOL4 min read 72.9K   1.2K   68  
The present document tries to describe the architecture of a specific layer of access to data for relational databases. This document tries to present/display a form to automate tasks of access to data.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace XPeriment.Persistence.PersistentObjects
{
    public class ReflectionUtility
    {
        public static bool IsNulable(PropertyInfo pi)
        {
            object[] attributes = pi.GetCustomAttributes(typeof(NullsAllowed), true);
            if (attributes.Length > 0)
                return true;
            return false;
        }

        public static string GetDBField(PropertyInfo pi)
        {
            object[] attributes = pi.GetCustomAttributes(typeof(MapToAttribute), true);
            if (attributes.Length > 0)
            {
                MapToAttribute mapto = (MapToAttribute)attributes[0];
                return mapto.DBReference;
            }
            return pi.Name;
        }

        public static Dictionary<string, object> GetNameMappings(object obj)
        {
            Dictionary<string, object> retval = new Dictionary<string, object>();
            Type objtype = obj.GetType();
            PropertyInfo[] props = objtype.GetProperties();
            foreach (PropertyInfo pi in props)
            {                
                if (!MarkedAsPrimaryKey(pi) && !MarkedAsIgnoreMapping(pi))
                {
                    object[] attributes = pi.GetCustomAttributes(typeof(MapToAttribute), true);
                    if (attributes.Length > 0)
                    {
                        MapToAttribute mapto = (MapToAttribute)attributes[0];
                        retval.Add(mapto.DBReference, pi.GetValue(obj, null));
                    }
                    else
                    {
                        retval.Add(pi.Name, pi.GetValue(obj, null));
                    }
                }
            }
            return retval;
        }

        public static Dictionary<string, object> GetValueMappings(object obj)
        {
            Dictionary<string, object> retval = new Dictionary<string, object>();
            Type objtype = obj.GetType();
            PropertyInfo[] props = objtype.GetProperties();
            foreach (PropertyInfo pi in props)
            {
                MapToAttribute[] attributes = (MapToAttribute[])pi.GetCustomAttributes(typeof(MapToAttribute), true);
                if (attributes.Length > 0)
                {
                    MapToAttribute mapto = attributes[0];
                    retval.Add(mapto.DBReference, pi.GetValue(obj, null));
                }
                else
                {
                    if (!ReflectionUtility.MarkedAsIgnoreMapping(pi))
                        retval.Add(pi.Name, pi.GetValue(obj, null));
                }
            }
            return retval;
        }

        public static string GetTargetTable(object obj)
        {
            Type t = obj.GetType();
            MapToAttribute map = (MapToAttribute)t.GetCustomAttributes(typeof(MapToAttribute), true)[0];
            return map.DBReference;
        }

        public static bool MarkedAsPrimaryKey(PropertyInfo pi)
        {
            object[] attributes = pi.GetCustomAttributes(typeof(PrimaryKeyAttribute),true);
            if (attributes.Length > 0)
                return true;
            return false;
        }

        public static bool MarkedAsForaignKey(PropertyInfo pi)
        {
            object[] attributes = pi.GetCustomAttributes(typeof(ForaignKey), true);
            if (attributes.Length > 0)
                return true;
            return false;
        }


        public static bool MarkedAsIgnoreMapping(PropertyInfo pi)
        {
            object[] attributes = pi.GetCustomAttributes(typeof(IgnoreMappingAttribute), true);
            if (attributes.Length > 0)
                return true;
            return false;
        }


        public static string GetPrimaryKeyField(object obj)
        {
            Type objtype = obj.GetType();
            PropertyInfo[] props = objtype.GetProperties();
            foreach (PropertyInfo pi in props)
            {
                PrimaryKeyAttribute[] attributes = (PrimaryKeyAttribute[])pi.GetCustomAttributes(typeof(PrimaryKeyAttribute), true);
                if (attributes.Length > 0)
                {
                    PrimaryKeyAttribute pk = attributes[0];
                    return GetDBField(pi);
                }
            }
            throw new Exception("Object of type '"+objtype.Name+"' has no primary keys defined");
        }

        public static object GetPrimaryKeyValue(object obj)
        {
            return "";
        }

        public static void SetValue(object obj, Dictionary<string, object> mappings)
        {

        }

    }
}

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
Systems Engineer
Colombia Colombia
http://www.construirsoftware.blogspot.com/

Comments and Discussions