首页 > 代码库 > TableInfo<T> or TypeInfo

TableInfo<T> or TypeInfo

    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 =http://www.mamicode.com/ reader[index];
                if(value =http://www.mamicode.com/= 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 =http://www.mamicode.com/ row[map.QueryName];
                if (value =http://www.mamicode.com/= DBNull.Value)
                    value = SSGClass.DataConvert.GetValue(value, value.GetType());
                delegateSet[map.PropertyName].Invoke(entity, value, null);
            }
            return entity;
        }
    }


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

 

    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 =http://www.mamicode.com/ reader[index];
                if (value =http://www.mamicode.com/= 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 =http://www.mamicode.com/ row[map.QueryName];
                if (value =http://www.mamicode.com/= DBNull.Value)
                    value = SSGClass.DataConvert.GetValue(value, value.GetType());
                delegateSet[map.PropertyName].Invoke(entity, value, null);
            }
            return entity;
        }

    }