/// <summary> /// 构造函数 /// </summary> /// <param name="masterTableMapping">主关联表的映射</param> /// <param name="relateTableType">关联类型</param> /// <param name="relationName">关联名</param> public RelationFieldMapping(DataEntityMapping masterTableMapping, Type relateTableType, string relationName) { _masterTableMapping = masterTableMapping; _relationName = relationName; if (relateTableType.IsArray) { _relationTableType = relateTableType.GetElementType(); _relationKind = RelationKind.OneToMany; _resultDataKind = DataKind.Array; } else if (relateTableType.IsGenericType) { Type frameType = relateTableType.GetGenericTypeDefinition(); if (frameType.FullName != "System.Collections.Generic.IList`1") { throw new LightDataException(RE.TheRelationTypeNotIList); } else { Type[] arguments = relateTableType.GetGenericArguments(); _relationTableType = arguments [0]; _relationKind = RelationKind.OneToMany; _resultDataKind = DataKind.IList; } } else { _relationTableType = relateTableType; _relationKind = RelationKind.OneToOne; _resultDataKind = DataKind.SingleObject; } }
public bool Equals(DataEntityMapping mapping) { if (mapping == null) { return(false); } return(this.ObjectType.Equals(mapping.ObjectType)); }
/// <summary> /// 获取关系映射图 /// </summary> /// <param name="type">映射类型</param> /// <returns>关系映射图</returns> public static DataEntityMapping GetEntityMapping(Type type) { DataEntityMapping dataMapping = GetMapping(type) as DataEntityMapping; if (dataMapping == null) { throw new LightDataException(RE.TheDataMappingIsNotDataEntityMapping); } else { return(dataMapping); } }
/// <summary> /// 创建关系映射图 /// </summary> /// <param name="type">映射类型</param> /// <returns>关系映射图</returns> private static DataMapping CreateMapping(Type type) { string tableName = null; string extentParam = null; bool isEntityTable = true; DataMapping dataMapping = null; IDataTableConfig config = ConfigManager.LoadDataTableConfig(type); if (config != null) { tableName = config.TableName; extentParam = config.ExtendParams; isEntityTable = config.IsEntityTable; } if (string.IsNullOrEmpty(tableName)) { tableName = type.Name; } if (type.IsSubclassOf(typeof(DataTableEntity))) { dataMapping = new DataTableEntityMapping(type, tableName, true); } else if (type.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); } } dataMapping.ExtentParams = new ExtendParamsCollection(extentParam); return(dataMapping); }
public static DataFieldMapping CreateDataFieldMapping(Type type, PropertyInfo property, string fieldName, string indexName, IDataFieldConfig config, DataEntityMapping mapping, Type mainType) { if (!Regex.IsMatch(fieldName, _fieldRegex, RegexOptions.IgnoreCase)) { throw new LightDataException(RE.FieldNameIsInvalid); } DataFieldMapping fieldMapping = null; bool isNullable = false; string dbType = config.DBType; if (type.IsGenericType) { Type frameType = type.GetGenericTypeDefinition(); if (frameType.FullName == "System.Nullable`1") { Type[] arguments = type.GetGenericArguments(); type = arguments [0]; isNullable = true; } } isNullable = isNullable || config.IsNullable; if (type.IsArray && type.FullName != "System.Byte[]") { throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight); } else if (type.IsGenericParameter | type.IsGenericTypeDefinition) { throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight); } else if (type.IsEnum) { EnumFieldMapping enumFieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType); // enumFieldMapping.IsNullable = config.IsNullable; if (config.DefaultValue != null) { if (config.DefaultValue is String) { enumFieldMapping.DefaultValue = Enum.Parse(type, config.DefaultValue as String, true); } else { Array arr = Enum.GetValues(type); foreach (object obj in arr) { if (obj.Equals(config.DefaultValue)) { enumFieldMapping.DefaultValue = config.DefaultValue; break; } } } } fieldMapping = enumFieldMapping; } else { TypeCode code = Type.GetTypeCode(type); if (code == TypeCode.DBNull) { throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight); } if (code == TypeCode.Empty) { throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight); } else if (code == TypeCode.Object && type.FullName != "System.Byte[]") { ComplexFieldMapping complexFieldMapping = new ComplexFieldMapping(type, fieldName, indexName, mapping, isNullable); // complexFieldMapping.IsNullable = config.IsNullable; fieldMapping = complexFieldMapping; } else { PrimitiveFieldMapping primitiveFieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType); primitiveFieldMapping.IsIdentity = config.IsIdentity; primitiveFieldMapping.IsPrimaryKey = config.IsPrimaryKey; // primitiveFieldMapping.IsNullable = config.IsNullable; // primitiveFieldMapping.DBType = config.DBType; if (config.DefaultValue != null) { if (config.DefaultValue.GetType() == type) { primitiveFieldMapping.DefaultValue = config.DefaultValue; } else { primitiveFieldMapping.DefaultValue = Convert.ChangeType(config.DefaultValue, type); } } fieldMapping = primitiveFieldMapping; } } // if (isNullAbleType) { // fieldMapping.IsNullable = true; // } if (fieldMapping.IsNullable) { PropertyInfo specifiedProperty = GetSpecifiedProperty(property.Name + "Specified", mapping.ObjectType); if (specifiedProperty != null) { fieldMapping._specifiedHandler = new PropertyHandler(specifiedProperty); } } if (config.DataOrder > 0) { fieldMapping.DataOrder = config.DataOrder - 1; } return(fieldMapping); }
public CustomFieldMapping(string fieldName, DataEntityMapping mapping) : base(null, fieldName, null, mapping, false, null) { // Name = fieldName; // TypeMapping = mapping; }