Ejemplo n.º 1
0
        /// <summary>
        /// 使用实体类为命令追加参数
        /// </summary>
        public static void AppendEntityParameters <T>(this DbCommand command, T obj) where T : BaseEntity
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            TableMapper tableMapper = MappingCachePool.GetOrAdd <T>();
            var         columns     = tableMapper.Columns;

            foreach (var item in columns)
            {
                DbParameter parameter = command.CreateParameter();
                parameter.ParameterName = "@" + item.Value.PropertyName;
                object value = item.Value.GetPropertyValue(obj);
                if (value == null)
                {
                    parameter.Value = DBNull.Value;
                }
                else
                {
                    BaseValueConvert valueConvert = item.Value.ValueConvert as BaseValueConvert;
                    if (valueConvert != null)
                    {
                        parameter.Value = valueConvert.Write(value);
                    }
                    else
                    {
                        if (item.Value.IsEnum)
                        {
                            parameter.Value = Convert.ToInt16(value);
                        }
                        else if (item.Value.IsGuidString)
                        {
                            parameter.Value = Guid.Parse(value.ToString());
                        }
                        else
                        {
                            parameter.Value = value;
                        }
                    }
                }
                command.Parameters.Add(parameter);
            }
        }
Ejemplo n.º 2
0
        public static TableMapper ToMapper <T>() where T : BaseEntity
        {
            Type           type           = typeof(T);
            TableAttribute tableAttribute = type.GetCustomAttribute <TableAttribute>();
            string         tableName;

            if (tableAttribute != null && !string.IsNullOrWhiteSpace(tableAttribute.Name))
            {
                tableName = tableAttribute.Name.Trim();
            }
            else
            {
                tableName = GetName(type.Name);
            }
            TableMapper tableMapper = new TableMapper()
            {
                TableName = tableName, Type = type, TypeName = type.Name
            };
            List <PropertyInfo> pList = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();

            foreach (PropertyInfo property in pList)
            {
                // 检查 IgnoreAttribute
                IgnoreAttribute ignore = property.GetCustomAttribute <IgnoreAttribute>();
                if (ignore != null)
                {
                    continue;
                }
                // 检查 ColumnAttribute
                bool         isKey;
                bool         isAuto = false;
                KeyAttribute key    = property.GetCustomAttribute <KeyAttribute>();
                if (key != null)
                {
                    isKey  = true;
                    isAuto = key.IsAuto;
                }
                else if (string.Equals(property.Name, "id", StringComparison.OrdinalIgnoreCase))
                {
                    // 当属性名为ID,但又未指定Key特性时,按以下规则判断,如果是整数,则为自动增量,否则不是自动增量。
                    string propertyTypeName = property.PropertyType.FullName;
                    if (propertyTypeName == "System.Int32" || propertyTypeName == "System.UInt32" || propertyTypeName == "System.Int64" || propertyTypeName == "System.UInt64")
                    {
                        isAuto = true;
                    }
                    else
                    {
                        isAuto = false;
                    }
                    isKey = true;
                }
                else
                {
                    isKey  = false;
                    isAuto = false;
                }
                ColumnAttribute  columnAttribute = property.GetCustomAttribute <ColumnAttribute>();
                IndexAttribute   indexAttribute  = property.GetCustomAttribute <IndexAttribute>();
                string           columnName      = "";
                BaseValueConvert valueConvert    = null;
                if (columnAttribute != null)
                {
                    // 获取列名
                    if (!string.IsNullOrWhiteSpace(columnAttribute.Name))
                    {
                        columnName = columnAttribute.Name.Trim();
                    }
                    else
                    {
                        columnName = GetName(property.Name);
                    }
                    // 获取 ValueConvert
                    if (columnAttribute.Convert != null)
                    {
                        Type convertType = columnAttribute.Convert;
                        if (convertType.IsAbstract)
                        {
                            throw new Exception($"{property.Name} value convert cannot be an abstract class.");
                        }
                        if (!convertType.IsSubclassOf(typeof(BaseValueConvert)))
                        {
                            string name = typeof(BaseValueConvert).Name;
                            throw new Exception($"{property.Name} value convert must inherit {name}.");
                        }
                        valueConvert = Activator.CreateInstance(convertType) as BaseValueConvert;
                    }
                }
                else
                {
                    columnName = GetName(property.Name);
                }
                // 创建映射实例
                ColumnMapper columnMapper = new ColumnMapper()
                {
                    ColumnName     = columnName,
                    IsPrimarykey   = isKey,
                    PropertyInfo   = property,
                    PropertyName   = property.Name,
                    ValueConvert   = valueConvert,
                    IsAuto         = isAuto,
                    IsEnum         = property.PropertyType.IsEnum,
                    IsGuidString   = property.PropertyType == typeof(string) && columnName.ToLower().Contains("id"),
                    IsCanNull      = IsNullable(property.PropertyType),
                    IsIndex        = indexAttribute != null,
                    DbType         = (columnAttribute != null && !string.IsNullOrWhiteSpace(columnAttribute.DbType)) ? columnAttribute.DbType : "",
                    DbDefaultValue = (columnAttribute != null && !string.IsNullOrWhiteSpace(columnAttribute.DbDefaultValue)) ? columnAttribute.DbDefaultValue : ""
                };
                tableMapper.Add(columnMapper);
            }
            return(tableMapper);
        }