Пример #1
0
        /// <summary>
        /// 从字段映射中获取DataParameter集合
        /// </summary>
        /// <param name="mappings">字段映射列表</param>
        /// <param name="source">数据对象</param>
        /// <returns>DataParameter集合</returns>
        protected virtual List <DataParameter> GetDataParameters(IEnumerable <FieldMapping> mappings, object source)
        {
            List <DataParameter> paramList = new List <DataParameter> ();

            foreach (DataFieldMapping field in mappings)
            {
                if (field is PrimitiveFieldMapping)
                {
                    PrimitiveFieldMapping primitiveFieldMapping = field as PrimitiveFieldMapping;

                    object obj = primitiveFieldMapping.Handler.Get(source);
                    if (primitiveFieldMapping.SpecifiedHandler != null)
                    {
                        bool hasdata = Convert.ToBoolean(primitiveFieldMapping.SpecifiedHandler.Get(source));
                        if (!hasdata)
                        {
                            obj = null;
                        }
                    }
                    if (obj == null && !primitiveFieldMapping.IsNullable)
                    {
                        throw new LightDataException(string.Format(RE.DataValueIsNotAllowEmply, primitiveFieldMapping.Name));
                    }
                    DataParameter dataParameter = new DataParameter(field.Name, field.ToColumn(obj), field.DBType, ParameterDirection.Input);
                    paramList.Add(dataParameter);
                }
                else if (field is EnumFieldMapping)
                {
                    EnumFieldMapping enumFieldMapping = field as EnumFieldMapping;
                    object           obj = enumFieldMapping.Handler.Get(source);
                    if (enumFieldMapping.SpecifiedHandler != null)
                    {
                        bool hasdata = Convert.ToBoolean(enumFieldMapping.SpecifiedHandler.Get(source));
                        if (!hasdata)
                        {
                            obj = null;
                        }
                    }
                    if (obj == null && !enumFieldMapping.IsNullable)
                    {
                        throw new LightDataException(string.Format(RE.DataValueIsNotAllowEmply, enumFieldMapping.Name));
                    }
                    DataParameter dataParameter = new DataParameter(field.Name, field.ToColumn(obj), field.DBType, ParameterDirection.Input);
                    paramList.Add(dataParameter);
                }
                else if (field is ComplexFieldMapping)
                {
                    ComplexFieldMapping complexFieldMapping = field as ComplexFieldMapping;
                    object obj = complexFieldMapping.Handler.Get(source);
                    if (obj == null)
                    {
                        throw new LightDataException(string.Format(RE.DataValueIsNotAllowEmply, complexFieldMapping.Name));
                    }
                    List <DataParameter> subParamList = GetDataParameters(complexFieldMapping.GetFieldMappings(), obj);
                    paramList.AddRange(subParamList);
                }
            }
            return(paramList);
        }
Пример #2
0
        public static DataFieldMapping CreateDataFieldMapping(PropertyInfo property, DataFieldMapperConfig config,
                                                              int positionOrder, DataMapping mapping)
        {
            var type      = property.PropertyType;
            var typeInfo  = type.GetTypeInfo();
            var indexName = property.Name;
            var fieldName = string.IsNullOrEmpty(config.Name) ? property.Name : config.Name;

            DataFieldMapping fieldMapping;

            if (typeInfo.IsGenericType)
            {
                var frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    var arguments = typeInfo.GetGenericArguments();
                    type     = arguments[0];
                    typeInfo = type.GetTypeInfo();
                }
            }

            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    fieldMapping = new BytesFieldMapping(fieldName, indexName, mapping,
                                                         config.IsNullable, config.DbType, config.IsIdentity, config.IsPrimaryKey);
                }
                else
                {
                    fieldMapping = new ObjectFieldMapping(type, fieldName, indexName, mapping,
                                                          config.IsNullable, config.DbType, config.IsIdentity, config.IsPrimaryKey);
                }
            }
            // else if (type.IsGenericParameter || typeInfo.IsGenericTypeDefinition)
            // {
            //     throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType,
            //         property.Name, type));
            // }
            else if (type == typeof(Guid))
            {
                fieldMapping = new GuidFieldMapping(fieldName, indexName, mapping, config.IsNullable,
                                                    config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type == typeof(string))
            {
                fieldMapping = new StringFieldMapping(fieldName, indexName, mapping, config.IsNullable,
                                                      config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type == typeof(DateTime))
            {
                fieldMapping = new DateTimeFieldMapping(fieldName, indexName, mapping, config.IsNullable,
                                                        config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type == typeof(decimal))
            {
                fieldMapping = new DecimalFieldMapping(fieldName, indexName, mapping, config.IsNullable,
                                                       config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (typeInfo.IsEnum)
            {
                fieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, config.IsNullable,
                                                    config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type.IsPrimitive)
            {
                fieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping,
                                                         config.IsNullable, config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type == typeof(DBNull))
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType,
                                                           property.Name, type));
            }
            else
            {
                fieldMapping = new ObjectFieldMapping(type, fieldName, indexName, mapping,
                                                      config.IsNullable, config.DbType, config.IsIdentity, config.IsPrimaryKey);
            }

            if (config.DataOrder > 0)
            {
                fieldMapping.DataOrder = config.DataOrder;
            }

            fieldMapping.PositionOrder   = positionOrder;
            fieldMapping.FunctionControl = config.FunctionControl == FunctionControl.Default
                ? FunctionControl.Full
                : config.FunctionControl;
            fieldMapping.Handler = new PropertyHandler(property);
            return(fieldMapping);
        }
Пример #3
0
        public static DataFieldMapping CreateCustomFieldMapping(PropertyInfo property, DataMapping mapping)
        {
            Type     type      = property.PropertyType;
            TypeInfo typeInfo  = type.GetTypeInfo();
            string   indexName = property.Name;
            string   fieldName = property.Name;

            DataFieldMapping fieldMapping;
            string           dbType     = null;
            bool             isNullable = false;

            if (typeInfo.IsGenericType)
            {
                Type frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    Type[] arguments = typeInfo.GetGenericArguments();
                    type       = arguments[0];
                    typeInfo   = type.GetTypeInfo();
                    isNullable = true;
                }
            }

            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    BytesFieldMapping bytesFieldMapping = new BytesFieldMapping(type, fieldName, indexName, mapping, isNullable, null);
                    fieldMapping = bytesFieldMapping;
                }
                else
                {
                    return(null);
                }
            }
            else if (type.IsGenericParameter || typeInfo.IsGenericTypeDefinition)
            {
                return(null);
            }
            else if (typeInfo.IsEnum)
            {
                EnumFieldMapping enumFieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, isNullable);
                fieldMapping = enumFieldMapping;
            }
            else
            {
                TypeCode code = Type.GetTypeCode(type);
                if (code == TypeCode.Empty)
                {
                    return(null);
                }
                else if (code == TypeCode.Object)
                {
                    ObjectFieldMapping objectFieldMapping = new ObjectFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType);
                    fieldMapping = objectFieldMapping;
                }
                else
                {
                    PrimitiveFieldMapping primitiveFieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType, null, false, false);
                    fieldMapping = primitiveFieldMapping;
                }
            }
            fieldMapping._handler = new PropertyHandler(property);
            return(fieldMapping);
        }
Пример #4
0
        public static DataFieldMapping CreateCustomFieldMapping(PropertyInfo property, DataMapping mapping)
        {
            var type      = property.PropertyType;
            var typeInfo  = type.GetTypeInfo();
            var indexName = property.Name;
            var fieldName = property.Name;

            DataFieldMapping fieldMapping;
            var isNullable = false;

            if (typeInfo.IsGenericType)
            {
                var frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    var arguments = typeInfo.GetGenericArguments();
                    type       = arguments[0];
                    typeInfo   = type.GetTypeInfo();
                    isNullable = true;
                }
            }

            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    fieldMapping = new BytesFieldMapping(fieldName, indexName, mapping, isNullable, null, false,
                                                         false);
                }
                else
                {
                    fieldMapping =
                        new ObjectFieldMapping(type, fieldName, indexName, mapping, isNullable, null, false, false);
                }
            }

            else if (type == typeof(Guid))
            {
                fieldMapping =
                    new GuidFieldMapping(fieldName, indexName, mapping, isNullable, null, null, false, false);
            }
            else if (type == typeof(string))
            {
                fieldMapping =
                    new StringFieldMapping(fieldName, indexName, mapping, isNullable, null, null, false, false);
            }
            else if (type == typeof(DateTime))
            {
                fieldMapping =
                    new DateTimeFieldMapping(fieldName, indexName, mapping, isNullable, null, null, false, false);
            }
            else if (type == typeof(decimal))
            {
                fieldMapping =
                    new DecimalFieldMapping(fieldName, indexName, mapping, isNullable, null, null, false, false);
            }
            else if (typeInfo.IsEnum)
            {
                fieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, isNullable, null, null, false,
                                                    false);
            }
            else if (type.IsPrimitive)
            {
                fieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping,
                                                         isNullable, null, null, false, false);
            }
            else if (type == typeof(DBNull))
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType,
                                                           property.Name, type));
            }
            else
            {
                fieldMapping =
                    new ObjectFieldMapping(type, fieldName, indexName, mapping, isNullable, null, false, false);
            }

            fieldMapping.Handler = new PropertyHandler(property);
            return(fieldMapping);
        }
Пример #5
0
        public static DataFieldMapping CreateDataFieldMapping(PropertyInfo property, DataFieldMapperConfig config, int positionOrder, DataMapping mapping)
        {
            Type     type      = property.PropertyType;
            TypeInfo typeInfo  = type.GetTypeInfo();
            string   indexName = property.Name;
            string   fieldName = string.IsNullOrEmpty(config.Name) ? property.Name : config.Name;
            //if (!Regex.IsMatch(fieldName, _fieldRegex, RegexOptions.IgnoreCase)) {
            //    throw new LightDataException(string.Format(SR.FieldNameIsInvalid, type, fieldName));
            //}

            DataFieldMapping fieldMapping;

            if (typeInfo.IsGenericType)
            {
                Type frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    Type[] arguments = typeInfo.GetGenericArguments();
                    type     = arguments[0];
                    typeInfo = type.GetTypeInfo();
                }
            }
            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    BytesFieldMapping bytesFieldMapping = new BytesFieldMapping(type, fieldName, indexName, mapping, config.IsNullable, config.DbType);
                    fieldMapping = bytesFieldMapping;
                }
                else
                {
                    throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType, property.Name, type));
                }
            }
            else if (type.IsGenericParameter || typeInfo.IsGenericTypeDefinition)
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType, property.Name, type));
            }
            else if (typeInfo.IsEnum)
            {
                EnumFieldMapping enumFieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, config.IsNullable, config.DbType, config.DefaultValue);
                fieldMapping = enumFieldMapping;
            }
            else
            {
                TypeCode code = Type.GetTypeCode(type);
                if (code == TypeCode.Empty)
                {
                    throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType, property.Name, type));
                }
                else if (code == TypeCode.Object)
                {
                    ObjectFieldMapping objectFieldMapping = new ObjectFieldMapping(type, fieldName, indexName, mapping, config.IsNullable, config.DbType);
                    fieldMapping = objectFieldMapping;
                }
                else
                {
                    PrimitiveFieldMapping primitiveFieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping, config.IsNullable, config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
                    fieldMapping = primitiveFieldMapping;
                }
            }
            if (config.DataOrder > 0)
            {
                fieldMapping._dataOrder = config.DataOrder;
            }
            fieldMapping._positionOrder   = positionOrder;
            fieldMapping._functionControl = config.FunctionControl == FunctionControl.Default ? FunctionControl.Full : config.FunctionControl;
            fieldMapping._handler         = new PropertyHandler(property);
            return(fieldMapping);
        }