/// <summary> /// 将DataReader的值写入到对象中 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="dr">IDataReader对象</param> /// <param name="items">映射关系</param> /// <param name="graph">对象</param> /// <param name="dod"></param> public static void DataReaderToObject <T>(IDataReader dr, ORMappingItemCollection items, T graph, DataToObjectDeligations dod) { ExceptionHelper.FalseThrow <ArgumentNullException>(dr != null, "dr"); ExceptionHelper.FalseThrow <ArgumentNullException>(items != null, "items"); ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); DataTable schemaTable = dr.GetSchemaTable(); using (ORMappingContext context = ORMappingContext.GetContext()) { foreach (DataRow row in schemaTable.Rows) { string columnName = row["ColumnName"].ToString(); if (items.ContainsKey(columnName)) { ORMappingItem item = items[row["ColumnName"].ToString()]; System.Type realType = GetRealType(item.MemberInfo); object data = dr[columnName]; if (item.EncryptProperty) { data = DecryptPropertyValue(item, data); } if (Convertible(realType, data)) { SetValueToObject(item, graph, ConvertData(item, data), dr, dod); } } } } }
/// <summary> /// /// </summary> /// <param name="row">DataRow对象</param> /// <param name="items">映射关系</param> /// <param name="graph">对象</param> /// <param name="dod"></param> public static void DataRowToObject(DataRow row, ORMappingItemCollection items, object graph, DataToObjectDeligations dod) { ExceptionHelper.FalseThrow <ArgumentNullException>(row != null, "row"); ExceptionHelper.FalseThrow <ArgumentNullException>(items != null, "items"); ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); ExceptionHelper.FalseThrow <ArgumentNullException>(row.Table != null, "row.Table"); using (ORMappingContext context = ORMappingContext.GetContext()) { foreach (DataColumn column in row.Table.Columns) { if (items.ContainsKey(column.ColumnName)) { ORMappingItem item = items[column.ColumnName]; System.Type realType = GetRealType(item.MemberInfo); object data = row[column]; if (item.EncryptProperty) { data = DecryptPropertyValue(item, data); } if (Convertible(realType, data)) { SetValueToObject(item, graph, ConvertData(item, data), row, dod); } } } } }
private static ORMappingItemCollection GetMappingItemsBySubClass(RelativeAttributes attrs, MemberInfo sourceMI) { ORMappingItemCollection items = new ORMappingItemCollection(); System.Type subType = attrs.SubClassType != null ? attrs.SubClassType.Type : GetRealType(sourceMI); MemberInfo[] mis = GetTypeMembers(subType); foreach (SubClassORFieldMappingAttribute attr in attrs.SubClassFieldMappings) { MemberInfo mi = GetMemberInfoByName(attr.SubPropertyName, mis); if (mi != null) { if (items.ContainsKey(attr.DataFieldName) == false) { ORMappingItem item = new ORMappingItem(); item.PropertyName = sourceMI.Name; item.SubClassPropertyName = attr.SubPropertyName; item.MemberInfo = mi; item.DeclaringType = sourceMI.DeclaringType; if (attrs.SubClassType != null) { item.SubClassTypeDescription = attrs.SubClassType.TypeDescription; } FillMappingItemByAttr(item, attr); items.Add(item); } } } foreach (SubClassSqlBehaviorAttribute attr in attrs.SubClassFieldSqlBehaviors) { ORMappingItem item = FindItemBySubClassPropertyName(attr.SubPropertyName, items); if (item != null) { FillMappingItemByBehaviorAttr(item, attr); } } foreach (SubClassPropertyEncryptionAttribute attr in attrs.SubClassPropertyEncryptions) { ORMappingItem item = FindItemBySubClassPropertyName(attr.SubPropertyName, items); if (item != null) { FillMappingItemByEncryptionAttr(item, attr); } } return(items); }
private static void MergeMappingItems(ORMappingItemCollection dest, ORMappingItemCollection src) { foreach (ORMappingItem item in src) { if (dest.ContainsKey(item.DataFieldName) == false) { dest.Add(item); } } }