internal static bool TryGetRelateFieldConfig(Type type, string fieldName, out RelationFieldMapConfig config)
 {
     CheckData();
     while (true)
     {
         if (settingDict.TryGetValue(type, out DataTableMapperSetting setting))
         {
             config = setting.GetRelationFieldMapConfig(fieldName);
             if (config != null)
             {
                 return(true);
             }
         }
         type = type.BaseType;
         if (type.Equals(typeof(object)) || type.Equals(null))
         {
             config = null;
             return(false);
         }
     }
 }
        public static RelationFieldMapConfig LoadRelationDataFieldConfig(Type type, PropertyInfo pi)
        {
            if (DataMapperConfiguration.TryGetRelateFieldConfig(type, pi.Name, out var mapperConfig))
            {
                return(mapperConfig);
            }

            var relationAttributes = AttributeCore.GetPropertyAttributes <RelationFieldAttribute>(pi, true);

            if (relationAttributes.Length > 0)
            {
                var rfConfig = new RelationFieldMapConfig(pi.Name);
                foreach (var ra in relationAttributes)
                {
                    rfConfig.AddRelationKeys(ra.MasterKey, ra.RelateKey);
                }
                return(rfConfig);
            }

            return(null);
        }
Beispiel #3
0
        private void InitialRelationField()
        {
            PropertyInfo[] propertys = ObjectTypeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List <CollectionRelationFieldMapping> collectionTmpList = new List <CollectionRelationFieldMapping>();
            List <SingleRelationFieldMapping>     singleTmpList     = new List <SingleRelationFieldMapping>();

            foreach (PropertyInfo pi in propertys)
            {
                RelationFieldMapConfig config = MapperConfigManager.LoadRelationDataFieldConfig(ObjectType, pi);
                if (config != null && config.RelationKeyCount > 0)
                {
                    Type     type     = pi.PropertyType;
                    TypeInfo typeInfo = type.GetTypeInfo();
                    if (typeInfo.IsGenericType)
                    {
                        Type frameType = type.GetGenericTypeDefinition();
                        if (frameType == LCollectionFrameType || frameType.FullName == "System.Collections.Generic.ICollection`1")
                        {
                            Type[] arguments = typeInfo.GetGenericArguments();
                            type = arguments[0];
                            PropertyHandler handler  = new PropertyHandler(pi);
                            RelationKey[]   keypairs = config.GetRelationKeys();
                            CollectionRelationFieldMapping rmapping = new CollectionRelationFieldMapping(pi.Name, this, type, keypairs, handler);
                            collectionTmpList.Add(rmapping);
                        }
                    }
                    else
                    {
                        PropertyHandler            handler  = new PropertyHandler(pi);
                        RelationKey[]              keypairs = config.GetRelationKeys();
                        SingleRelationFieldMapping rmapping = new SingleRelationFieldMapping(pi.Name, this, type, keypairs, handler);
                        singleTmpList.Add(rmapping);
                    }
                }
            }
            _collectionRelationFields = new ReadOnlyCollection <CollectionRelationFieldMapping>(collectionTmpList);
            _singleRelationFields     = new ReadOnlyCollection <SingleRelationFieldMapping>(singleTmpList);
        }
 public void AddRelationFieldMapConfig(string fieldName, RelationFieldMapConfig config)
 {
     this.relationFieldMapConfigDict.Add(fieldName, config);
 }
        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;
                        }
                    }
                }
            }
        }