コード例 #1
0
        public static DataTableMapperConfig LoadDataTableConfig(Type type)
        {
            if (DataMapperConfiguration.TryGetSetting(type, out var setting))
            {
                return(setting.DataTableMapConfig);
            }

            var attributes = AttributeCore.GetTypeAttributes <DataTableAttribute>(type, true);

            if (attributes.Length > 0)
            {
                var attribute       = attributes[0];
                var paramAttributes = AttributeCore.GetTypeAttributes <ConfigParamAttribute>(type, true);
                var configParam     = new ConfigParamSet();
                if (paramAttributes != null && paramAttributes.Length > 0)
                {
                    foreach (var extendAttribute in paramAttributes)
                    {
                        configParam.SetParamValue(extendAttribute.Name, extendAttribute.Value);
                    }
                }
                var config = new DataTableMapperConfig(type)
                {
                    TableName     = attribute.TableName,
                    IsEntityTable = attribute.IsEntityTable,
                    ConfigParams  = configParam
                };
                return(config);
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Creates the mapping.
        /// </summary>
        /// <returns>The mapping.</returns>
        /// <param name="type">Type.</param>
        private static DataEntityMapping CreateMapping(Type type)
        {
            TypeInfo          typeInfo = type.GetTypeInfo();
            string            tableName;
            bool              isEntityTable;
            DataEntityMapping dataMapping;

            DataTableMapperConfig config = MapperConfigManager.LoadDataTableConfig(type);

            if (config != null)
            {
                tableName     = config.TableName;
                isEntityTable = config.IsEntityTable;
            }
            else
            {
                throw new LightDataException(string.Format(SR.NoDataEntityConfig, type.Name));
            }

            if (string.IsNullOrEmpty(tableName))
            {
                tableName = type.Name;
            }

            if (typeInfo.IsSubclassOf(typeof(DataTableEntity)))
            {
                dataMapping = new DataTableEntityMapping(type, tableName, true, true);
            }
            else if (typeInfo.IsSubclassOf(typeof(DataEntity)))
            {
                dataMapping = new DataEntityMapping(type, tableName, true);
            }
            else
            {
                if (!isEntityTable)
                {
                    dataMapping = new DataEntityMapping(type, tableName, false);
                }
                else
                {
                    dataMapping = new DataTableEntityMapping(type, tableName, false, false);
                }
            }
            dataMapping.ExtentParams = config.ConfigParams;
            return(dataMapping);
        }
コード例 #3
0
 public DataTableMapperSetting(DataTableMapperConfig dataTableMapConfig)
 {
     this.dataTableMapConfig = dataTableMapConfig;
 }
コード例 #4
0
        static void LoadData(string configFilePath)
        {
            FileInfo fileInfo;

            if (UseEntryAssemblyDirectory)
            {
                fileInfo = FileHelper.GetFileInfo(configFilePath, out bool absolute);
                if (!fileInfo.Exists && !absolute)
                {
                    fileInfo = new FileInfo(configFilePath);
                }
            }
            else
            {
                fileInfo = new FileInfo(configFilePath);
            }
            if (fileInfo.Exists)
            {
                using (StreamReader reader = fileInfo.OpenText()) {
                    string  content = reader.ReadToEnd();
                    JObject dom     = JObject.Parse(content);
                    var     section = dom.GetValue("lightDataMapper");
                    if (section == null)
                    {
                        return;
                    }
                    var optionList = section.ToObject <LightMapperOptions>();
                    if (optionList != null && optionList.DataTypes != null && optionList.DataTypes.Length > 0)
                    {
                        int typeIndex = 0;
                        foreach (DataTypeSection typeConfig in optionList.DataTypes)
                        {
                            typeIndex++;
                            var typeName = typeConfig.Type;
                            if (typeName == null)
                            {
                                throw new LightDataException(string.Format(SR.ConfigDataTypeNameIsNull, typeIndex));
                            }
                            var dataType     = Type.GetType(typeName, true);
                            var dataTypeInfo = dataType.GetTypeInfo();
                            var dataTableMap = new DataTableMapperConfig(dataType);
                            var setting      = new DataTableMapperSetting(dataTableMap);

                            dataTableMap.TableName     = typeConfig.TableName;
                            dataTableMap.IsEntityTable = typeConfig.IsEntityTable.HasValue ? typeConfig.IsEntityTable.Value : true;
                            var configParam  = new ConfigParamSet();
                            var paramConfigs = typeConfig.ConfigParams;
                            if (paramConfigs != null && paramConfigs.Count > 0)
                            {
                                foreach (var paramConfig in paramConfigs)
                                {
                                    configParam.SetParamValue(paramConfig.Value, paramConfig.Value);
                                }
                            }
                            dataTableMap.ConfigParams = configParam;
                            var dataFieldConfigs = typeConfig.DataFields;

                            if (dataFieldConfigs != null && dataFieldConfigs.Length > 0)
                            {
                                int fieldIndex = 0;
                                foreach (var fieldConfig in dataFieldConfigs)
                                {
                                    fieldIndex++;
                                    var fieldName = fieldConfig.FieldName;
                                    if (fieldName == null)
                                    {
                                        throw new LightDataException(string.Format(SR.ConfigDataFieldNameIsNull, typeName, fieldIndex));
                                    }
                                    var property = dataTypeInfo.GetProperty(fieldName);
                                    if (property == null)
                                    {
                                        throw new LightDataException(string.Format(SR.ConfigDataFieldIsNotExists, typeName, fieldName));
                                    }

                                    object defaultValue;
                                    try {
                                        defaultValue = CreateDefaultValue(property.PropertyType, fieldConfig.DefaultValue);
                                    }
                                    catch (Exception ex) {
                                        throw new LightDataException(string.Format(SR.ConfigDataFieldLoadError, typeName, fieldName, ex.Message));
                                    }
                                    FunctionControl functionControl;
                                    try {
                                        functionControl = CreateFunctionControl(fieldConfig);
                                    }
                                    catch (Exception ex) {
                                        throw new LightDataException(string.Format(SR.ConfigDataFieldLoadError, typeName, fieldName, ex.Message));
                                    }
                                    var dataFieldMap = new DataFieldMapperConfig(fieldName)
                                    {
                                        Name            = fieldConfig.Name,
                                        IsPrimaryKey    = fieldConfig.IsPrimaryKey,
                                        IsIdentity      = fieldConfig.IsIdentity,
                                        DbType          = fieldConfig.DbType,
                                        DataOrder       = fieldConfig.DataOrder,
                                        IsNullable      = fieldConfig.IsNullable,
                                        DefaultValue    = defaultValue,
                                        FunctionControl = functionControl
                                    };
                                    setting.AddDataFieldMapConfig(fieldName, dataFieldMap);
                                }
                            }
                            var relationFieldConfigs = typeConfig.RelationFields;
                            if (relationFieldConfigs != null && relationFieldConfigs.Length > 0)
                            {
                                int fieldIndex = 0;
                                foreach (var fieldConfig in relationFieldConfigs)
                                {
                                    fieldIndex++;
                                    if (fieldConfig.RelationPairs != null && fieldConfig.RelationPairs.Length > 0)
                                    {
                                        var fieldName = fieldConfig.FieldName;
                                        if (fieldName == null)
                                        {
                                            throw new LightDataException(string.Format(SR.ConfigDataFieldNameIsNull, typeName, fieldIndex));
                                        }
                                        var property = dataTypeInfo.GetProperty(fieldName);
                                        if (property == null)
                                        {
                                            throw new LightDataException(string.Format(SR.ConfigDataFieldIsNotExists, typeName, fieldName));
                                        }
                                        var dataFieldMap = new RelationFieldMapConfig(fieldName);
                                        foreach (var pair in fieldConfig.RelationPairs)
                                        {
                                            dataFieldMap.AddRelationKeys(pair.MasterKey, pair.RelateKey);
                                        }
                                        setting.AddRelationFieldMapConfig(fieldName, dataFieldMap);
                                    }
                                }
                            }
                            settingDict[dataType] = setting;
                        }
                    }
                }
            }
        }