示例#1
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);
        }
示例#2
0
        private void InitialRelationField()
        {
            var properties        = ObjectTypeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var collectionTmpList = new List <CollectionRelationFieldMapping>();
            var singleTmpList     = new List <SingleRelationFieldMapping>();

            foreach (var pi in properties)
            {
                var config = MapperConfigManager.LoadRelationDataFieldConfig(ObjectType, pi);
                if (config != null && config.RelationKeyCount > 0)
                {
                    var type     = pi.PropertyType;
                    var typeInfo = type.GetTypeInfo();
                    if (typeInfo.IsGenericType)
                    {
                        var frameType = type.GetGenericTypeDefinition();
                        if (frameType == LCollectionFrameType ||
                            frameType.FullName == "System.Collections.Generic.ICollection`1")
                        {
                            var arguments = typeInfo.GetGenericArguments();
                            type = arguments[0];
                            var handler  = new PropertyHandler(pi);
                            var keyPairs = config.GetRelationKeys();
                            var mapping  = new CollectionRelationFieldMapping(pi.Name, this, type, keyPairs, handler);
                            collectionTmpList.Add(mapping);
                        }
                    }
                    else
                    {
                        var handler  = new PropertyHandler(pi);
                        var keyPairs = config.GetRelationKeys();
                        var mapping  = new SingleRelationFieldMapping(pi.Name, this, type, keyPairs, handler);
                        singleTmpList.Add(mapping);
                    }
                }
            }

            _collectionRelationFields = new ReadOnlyCollection <CollectionRelationFieldMapping>(collectionTmpList);
            _singleRelationFields     = new ReadOnlyCollection <SingleRelationFieldMapping>(singleTmpList);
        }
示例#3
0
        protected void InitialDataFieldMapping()
        {
            var properties = ObjectTypeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var index      = 0;
            var list       = new List <FieldInfo>();

            foreach (var pi in properties)
            {
                var config = MapperConfigManager.LoadDataFieldConfig(ObjectType, pi);
                if (config != null)
                {
                    index++;
                    var info = new FieldInfo(pi, config, index);
                    list.Add(info);
                }
            }

            if (list.Count == 0)
            {
                throw new LightDataException(string.Format(SR.NoMappingField, ObjectType));
            }

            list.Sort((x, y) =>
            {
                if (x.DataOrder.HasValue && y.DataOrder.HasValue)
                {
                    if (x.DataOrder > y.DataOrder)
                    {
                        return(1);
                    }

                    if (x.DataOrder < y.DataOrder)
                    {
                        return(-1);
                    }

                    return(x.FieldOrder > y.FieldOrder ? 1 : -1);
                }

                if (x.DataOrder.HasValue && !y.DataOrder.HasValue)
                {
                    return(-1);
                }

                if (!x.DataOrder.HasValue && y.DataOrder.HasValue)
                {
                    return(1);
                }

                return(x.FieldOrder > y.FieldOrder ? 1 : -1);
            });
            var tmpList = new List <DataFieldMapping>();

            for (var i = 0; i < list.Count; i++)
            {
                var info    = list[i];
                var mapping = DataFieldMapping.CreateDataFieldMapping(info.Property, info.Config, i + 1, this);
                _fieldMappingDictionary.Add(mapping.IndexName, mapping);
                if (mapping.Name != mapping.IndexName)
                {
                    _fieldMappingDictionary.Add(mapping.Name, mapping);
                }

                tmpList.Add(mapping);
            }

            _fieldList = new ReadOnlyCollection <DataFieldMapping>(tmpList);
        }