码迷,mamicode.com
首页 > 其他好文 > 详细

TableInfo<T> or TypeInfo

时间:2014-05-19 09:29:01      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

bubuko.com,布布扣
    class TableInfo<T> where T : new()
    {
        public TableInfo()
        {
            Type type = typeof(T);
            TableAttribute tableattr = type.GetCustomAttributes(false).Where(attr => attr.GetType() == typeof(TableAttribute)).SingleOrDefault() as TableAttribute;
            if (tableattr != null)
                TableName = tableattr.Name;
            else
                TableName = type.Name;

            PropertyInfo[] infos = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            foreach (PropertyInfo info in infos)
            {
                object[] attributes = info.GetCustomAttributes(false);
                if (!attributes.Where(attr => attr.GetType() == typeof(IgnoreAttribute)).Any())
                {
                    KeyAttribute key = attributes.Where(attr => attr.GetType() == typeof(KeyAttribute)).SingleOrDefault() as KeyAttribute;
                    bool isKey = key != null;
                    if (isKey && key.IsIdentity) IdentityProperty = info.Name;
                    ColumnAttribute columnMap = attributes.Where(attr => attr.GetType() == typeof(ColumnAttribute)).SingleOrDefault() as ColumnAttribute;
                    string columnName = info.Name;
                    string queryName = columnName;
                    if (columnMap != null)
                    {
                        columnName = columnMap.Name;
                        queryName = columnMap.QueryName;
                    }
                    columns.Add(new ColumnMap(columnName, info.Name, queryName, isKey, !string.IsNullOrEmpty(IdentityProperty)));

                    delegateSet[info.Name] = info.SetValue;
                    delegateGet[info.Name] = info.GetValue;
                    selectText = "select " + GetColumnList() + " from " + TableName;

                    var s = InsertParams();
                    insertText = "insert into " + TableName + "(" + s[0] + ") values(" + s[1] + ")";
                    s = SetParams();
                    updateText = "update " + TableName + " set " + s[0] + " where " + s[1];
                }
            }
        }

        public string TableName { get; set; }
        string IdentityProperty = "";
        string selectText;
        string insertText;
        string updateText;
        List<ColumnMap> columns = new List<ColumnMap>();
        Dictionary<string, Action<object, object, object[]>> delegateSet = new Dictionary<string, Action<object, object, object[]>>();
        Dictionary<string, Func<object, object[], object>> delegateGet = new Dictionary<string, Func<object, object[], object>>();
        Dictionary<string, int> readerIndex = new Dictionary<string, int>();

        class ColumnMap
        {
            public ColumnMap()
            {
            }

            public ColumnMap(string propertyName)
            {
                ColumnName = propertyName;
                PropertyName = propertyName;
                QueryName = propertyName;
                IsPerimaryKey = false;
            }

            public ColumnMap(string propertyName, bool isPerimaryKey)
                : this(propertyName)
            {
                IsPerimaryKey = isPerimaryKey;
            }

            //public ColumnMap(string propertyName, bool isPerimaryKey,bool isIdentity)
            //    : this(propertyName, isPerimaryKey)
            //{
            //    IsIdentity = isIdentity;
            //}

            public ColumnMap(string columnName, string propertyName, string queryName, bool isPerimaryKey,bool isIdentity) : this(columnName,isPerimaryKey)
            {
                ColumnName = columnName;
                QueryName = queryName;
            }

            public string ColumnName { get; set; }
            public string PropertyName { get; set; }
            public string QueryName { get; set; }
            public bool IsPerimaryKey { get; set; }
            public bool IsIdentity { get; set; }
        }

        public string SelectText
        {
            get
            {
                return selectText;
            }
        }

        string GetColumnList()
        {
            List<string> columnList = new List<string>();
            foreach (ColumnMap map in columns)
            {
                if (map.ColumnName == map.QueryName)
                    columnList.Add(map.ColumnName);
                else
                    columnList.Add(map.ColumnName + " as " + map.QueryName);
            }
            return SSGClass.DataConvert.JoinList(columnList);
        }

        public string DeleteText
        {
            get
            {
                return "delete from " + TableName;
            }
        }

        public SSGClass.ExpressOpr GetDeleteExpress(T entity)
        {
            SSGClass.ExpressOpr exp = null;
            var keys = columns.Where(col => col.IsPerimaryKey == true);
            if (keys.Any())
            {
                foreach (var keyColumn in keys)
                {
                    if (exp == null)
                        exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
                    else
                        exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
                }
            }
            else
            {
                foreach (var keyColumn in columns)
                {
                    if (exp == null)
                        exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
                    else
                        exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
                }
            }
            return exp;
        }

        public string InsertText
        {
            get
            {
                return insertText;
            }
        }



        public Dictionary<string, object> GetEntityPatams(T entity)
        {
            Dictionary<string, object> paramValues = new Dictionary<string,object>();
            foreach (ColumnMap map in columns)
            {
                if (!string.IsNullOrEmpty(map.ColumnName))
                    paramValues.Add("@" + map.ColumnName, delegateGet[map.PropertyName].Invoke(entity, null));
            }
            return paramValues;
        }

        string[] InsertParams()
        {
            string[] param = new string[2];
            List<string> columnList = new List<string>();
            List<string> paramList = new List<string>();
            foreach (ColumnMap map in columns)
            {
                if (!string.IsNullOrEmpty(map.ColumnName))
                {
                    columnList.Add(map.ColumnName);
                    paramList.Add("@" + map.ColumnName);
                }
            }
            param[0] = SSGClass.DataConvert.JoinList(columnList);
            param[1] = SSGClass.DataConvert.JoinList(paramList);
            return param;
        }


        public string UpdateText
        {
            get
            {
                return updateText;
            }
        }

        string[] SetParams()
        {
            string[] param = new string[2];
            List<string> columnList = new List<string>();
            List<string> paramList = new List<string>();
            foreach (ColumnMap map in columns)
            {
                if (map.IsPerimaryKey)
                    paramList.Add(map.ColumnName + " = @" + map.ColumnName);
                else
                    columnList.Add(map.ColumnName + " = @" + map.ColumnName);
            }
            param[0] = SSGClass.DataConvert.JoinList(columnList);
            param[1] = SSGClass.DataConvert.JoinList(paramList);
            return param;
        }

        public T GenEntity(System.Data.IDataReader reader)
        {
            T entity = new T();
            foreach (ColumnMap map in columns)
            {
                int index;
                if (!readerIndex.TryGetValue(map.QueryName, out index))
                {
                    index = reader.GetOrdinal(map.QueryName);
                    readerIndex[map.QueryName] = index;
                }
                object value = reader[index];
                if(value == DBNull.Value)
                    value = SSGClass.DataConvert.GetValue(value,value.GetType());
                delegateSet[map.PropertyName].Invoke(entity, value, null);
            }
            return entity;
        }

        public T GenEntity(System.Data.DataRow row)
        {
            T entity = new T();
            foreach (ColumnMap map in columns)
            {
                object value = row[map.QueryName];
                if (value == DBNull.Value)
                    value = SSGClass.DataConvert.GetValue(value, value.GetType());
                delegateSet[map.PropertyName].Invoke(entity, value, null);
            }
            return entity;
        }
    }
bubuko.com,布布扣


第一版本的泛型发射信息设计,后来发觉每次建立对象的时候都需要T信息,而object中必然存在Type信息,而通过Type来读写信息本来就是核心的功能,因此修改为TypeInfo

 

bubuko.com,布布扣
    class TypeInfo
    {
        public TypeInfo()
        {

        }
        public TypeInfo(Type type)
        {
            TableAttribute tableattr = type.GetCustomAttributes(false).Where(attr => attr.GetType() == typeof(TableAttribute)).SingleOrDefault() as TableAttribute;
            if (tableattr != null)
                TableName = tableattr.Name;
            else
                TableName = type.Name;

            PropertyInfo[] infos = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            foreach (PropertyInfo info in infos)
            {
                object[] attributes = info.GetCustomAttributes(false);
                if (!attributes.Where(attr => attr.GetType() == typeof(IgnoreAttribute)).Any())
                {
                    KeyAttribute key = attributes.Where(attr => attr.GetType() == typeof(KeyAttribute)).SingleOrDefault() as KeyAttribute;
                    bool isKey = key != null;
                    if (isKey && key.IsIdentity) IdentityProperty = info.Name;
                    ColumnAttribute columnMap = attributes.Where(attr => attr.GetType() == typeof(ColumnAttribute)).SingleOrDefault() as ColumnAttribute;
                    string columnName = info.Name;
                    string queryName = columnName;
                    if (columnMap != null)
                    {
                        columnName = columnMap.Name;
                        queryName = columnMap.QueryName;
                    }
                    columns.Add(new ColumnMap(columnName, info.Name, queryName, isKey, !string.IsNullOrEmpty(IdentityProperty)));

                    delegateSet[info.Name] = info.SetValue;
                    delegateGet[info.Name] = info.GetValue;
                    selectText = "select " + GetColumnList() + " from " + TableName;

                    var s = InsertParams();
                    insertText = "insert into " + TableName + "(" + s[0] + ") values(" + s[1] + ")";
                    s = SetParams();
                    updateText = "update " + TableName + " set " + s[0] + " where " + s[1];
                }
            }
        }

        public string TableName { get; set; }
        string IdentityProperty = "";
        string selectText;
        string insertText;
        string updateText;
        List<ColumnMap> columns = new List<ColumnMap>();
        Dictionary<string, Action<object, object, object[]>> delegateSet = new Dictionary<string, Action<object, object, object[]>>();
        Dictionary<string, Func<object, object[], object>> delegateGet = new Dictionary<string, Func<object, object[], object>>();
        Dictionary<string, int> readerIndex = new Dictionary<string, int>();

        class ColumnMap
        {
            public ColumnMap()
            {
            }

            public ColumnMap(string propertyName)
            {
                ColumnName = propertyName;
                PropertyName = propertyName;
                QueryName = propertyName;
                IsPerimaryKey = false;
            }

            public ColumnMap(string propertyName, bool isPerimaryKey)
                : this(propertyName)
            {
                IsPerimaryKey = isPerimaryKey;
            }

            public ColumnMap(string columnName, string propertyName, string queryName, bool isPerimaryKey, bool isIdentity)
                : this(columnName, isPerimaryKey)
            {
                ColumnName = columnName;
                QueryName = queryName;
            }

            public string ColumnName { get; set; }
            public string PropertyName { get; set; }
            public string QueryName { get; set; }
            public bool IsPerimaryKey { get; set; }
            public bool IsIdentity { get; set; }
        }

        public string SelectText
        {
            get
            {
                return selectText;
            }
        }

        string GetColumnList()
        {
            List<string> columnList = new List<string>();
            foreach (ColumnMap map in columns)
            {
                if (map.ColumnName == map.QueryName)
                    columnList.Add(map.ColumnName);
                else
                    columnList.Add(map.ColumnName + " as " + map.QueryName);
            }
            return SSGClass.DataConvert.JoinList(columnList);
        }

        public string DeleteText
        {
            get
            {
                return "delete from " + TableName;
            }
        }

        public SSGClass.ExpressOpr GetDeleteExpress(object entity)
        {
            SSGClass.ExpressOpr exp = null;
            var keys = columns.Where(col => col.IsPerimaryKey == true);
            if (keys.Any())
            {
                foreach (var keyColumn in keys)
                {
                    if (exp == null)
                        exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
                    else
                        exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
                }
            }
            else
            {
                foreach (var keyColumn in columns)
                {
                    if (exp == null)
                        exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
                    else
                        exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
                }
            }
            return exp;
        }

        public string InsertText
        {
            get
            {
                return insertText;
            }
        }



        public Dictionary<string, object> GetEntityPatams(object entity)
        {
            Dictionary<string, object> paramValues = new Dictionary<string, object>();
            foreach (ColumnMap map in columns)
            {
                if (!string.IsNullOrEmpty(map.ColumnName))
                    paramValues.Add("@" + map.ColumnName, delegateGet[map.PropertyName].Invoke(entity, null));
            }
            return paramValues;
        }

        string[] InsertParams()
        {
            string[] param = new string[2];
            List<string> columnList = new List<string>();
            List<string> paramList = new List<string>();
            foreach (ColumnMap map in columns)
            {
                if (!string.IsNullOrEmpty(map.ColumnName))
                {
                    columnList.Add(map.ColumnName);
                    paramList.Add("@" + map.ColumnName);
                }
            }
            param[0] = SSGClass.DataConvert.JoinList(columnList);
            param[1] = SSGClass.DataConvert.JoinList(paramList);
            return param;
        }


        public string UpdateText
        {
            get
            {
                return updateText;
            }
        }

        string[] SetParams()
        {
            string[] param = new string[2];
            List<string> columnList = new List<string>();
            List<string> paramList = new List<string>();
            foreach (ColumnMap map in columns)
            {
                if (map.IsPerimaryKey)
                    paramList.Add(map.ColumnName + " = @" + map.ColumnName);
                else
                    columnList.Add(map.ColumnName + " = @" + map.ColumnName);
            }
            param[0] = SSGClass.DataConvert.JoinList(columnList);
            param[1] = SSGClass.DataConvert.JoinList(paramList);
            return param;
        }

        public T GenEntity<T>(System.Data.IDataReader reader) where T : new()
        {
            T entity = new T();
            foreach (ColumnMap map in columns)
            {
                int index;
                if (!readerIndex.TryGetValue(map.QueryName, out index))
                {
                    index = reader.GetOrdinal(map.QueryName);
                    readerIndex[map.QueryName] = index;
                }
                object value = reader[index];
                if (value == DBNull.Value)
                    value = SSGClass.DataConvert.GetValue(value, value.GetType());
                delegateSet[map.PropertyName].Invoke(entity, value, null);
            }
            return entity;
        }

        public T GenEntity<T>(System.Data.DataRow row) where T : new()
        {
            T entity = new T();
            foreach (ColumnMap map in columns)
            {
                object value = row[map.QueryName];
                if (value == DBNull.Value)
                    value = SSGClass.DataConvert.GetValue(value, value.GetType());
                delegateSet[map.PropertyName].Invoke(entity, value, null);
            }
            return entity;
        }

    }
bubuko.com,布布扣

 

 

TableInfo<T> or TypeInfo,布布扣,bubuko.com

TableInfo<T> or TypeInfo

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/Kiver/p/ORM.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!