private ResultMappingItem CreateAssoMappingItem(DomainObject domainObject, DomainObjectElement element) { var item = new ResultMappingItem() { Property = element.PropertyName, ItemType = ResultMappingItemType.ResultMapping, }; return(item); }
private ResultMappingItem CreateCommonMappingItem(DomainObject domainObject, DomainObjectElement element) { var item = new ResultMappingItem() { NullValue = element.DefaultValue, Property = element.PropertyName, ItemType = ResultMappingItemType.Normal, Column = domainObject.DataObject.Columns.FirstOrDefault(i => i.ID == element.DataColumnID).ColumnName }; return(item); }
private ResultMappingItem CreateVirtualMappingItem(DomainObject domainObject, DomainObjectElement element) { var item = new ResultMappingItem() { NullValue = element.DefaultValue, Property = element.PropertyName, ItemType = ResultMappingItemType.Virtual, Column = null }; return(item); }
private ResultMappingItem CreateVirtualMappingItem(NSharding.DomainModel.Spi.DomainObject domainObject, DomainObjectElement element) { var item = new ResultMappingItem() { NullValue = element.DefaultValue, Property = element.PropertyName, ItemType = ResultMappingItemType.Virtual, Column = element.Alias, ParentDomainObjectId = domainObject.ID }; return(item); }
private ResultMappingItem CreateEnumMappingItem(NSharding.DomainModel.Spi.DomainObject domainObject, DomainObjectElement element) { var item = new ResultMappingItem() { NullValue = element.DefaultValue, Property = element.PropertyName, ItemType = ResultMappingItemType.Enum, Column = element.Alias, //Column = domainObject.DataObject.Columns.FirstOrDefault(i => i.ID == element.DataColumnID).ColumnName, TypeHandler = element.PropertyType, ParentDomainObjectId = domainObject.ID }; return(item); }
private ResultMappingItem CreateInnerAssoMapping(NSharding.DomainModel.Spi.DomainModel model, DomainObject currentDomainObject, Association association) { var item = new ResultMappingItem() { Property = association.PropertyName, TypeHandler = association.PropertyType, ItemType = ResultMappingItemType.SubResultMapping, ResultMapping = CreateDomainObjectMapping(currentDomainObject.Name, model, currentDomainObject), ParentDomainObjectId = currentDomainObject.ParentObjectID, CurrentDomainObjectId = currentDomainObject.ID, AssociationId = association.ID }; return(item); }
/// <summary> /// 循环构造结果集映射 /// </summary> /// <param name="parentObject">父对象</param> /// <param name="mapping">已有结果集映射</param> /// <param name="currentObject">当前对象</param> private void LoopCreateResultMapping(NSharding.DomainModel.Spi.DomainModel model, DomainObject parentObject, ResultMapping mapping, DomainObject currentObject) { var subMapping = CreateDomainObjectMapping(currentObject.Name, model, currentObject); var subMappingItem = new ResultMappingItem() { //Property = currentObject.PropertyName, LazyLoad = currentObject.IsLazyLoad, ResultMapping = subMapping, ItemType = ResultMappingItemType.SubResultMapping }; var innerAsso = parentObject.Associations.FirstOrDefault( i => i.AssociateType == AssociateType.InnerJoin && i.AssoDomainObjectID == currentObject.ID); if (innerAsso != null) { var parentElementID = innerAsso.Items.FirstOrDefault().SourceElementID; subMappingItem.Property = innerAsso.PropertyName; subMappingItem.GroupbyColumn = parentObject.DataObject.Columns.FirstOrDefault(c => c.ID == parentObject.Elements.FirstOrDefault(i => i.ID == parentElementID).DataColumnID).ColumnName; } mapping.MappingItems.Add(subMappingItem); //外键关联 var leftAssociations = parentObject.Associations.Where(i => i.AssociateType == AssociateType.OuterLeftJoin); foreach (var asso in leftAssociations) { var assoMapping = CreateAssociationMapping(asso); mapping.MappingItems.Add(new ResultMappingItem() { Property = asso.PropertyName, LazyLoad = asso.IsLazyLoad, ResultMapping = assoMapping, ItemType = ResultMappingItemType.ForeignResultMapping }); } if (currentObject.ChildDomainObjects.Count > 0) { foreach (var obj in currentObject.ChildDomainObjects) { LoopCreateResultMapping(model, currentObject, subMapping, obj); } } }
private void MappingCommonProperty(DataRow row, object obj, System.Reflection.PropertyInfo[] props, ResultMappingItem item, DomainObject domainObject) { try { var prop = props.FirstOrDefault(i => string.Equals(i.Name, item.Property, StringComparison.OrdinalIgnoreCase)); if (prop == null) { throw new ArgumentNullException("Prop:" + item.Property); } if (row[item.Column] != DBNull.Value) { prop.SetValue(obj, row[item.Column]); } } catch (Exception e) { e.Data.Add("PropertyName", item.Property); e.Data.Add("ColumnName", item.Column); throw e; } }
private void MappingCommonProperty(IDataReader reader, object obj, System.Reflection.PropertyInfo[] props, ResultMappingItem item) { try { var prop = props.FirstOrDefault(i => string.Equals(i.Name, item.Property, StringComparison.OrdinalIgnoreCase)); if (reader[item.Column] != DBNull.Value) { prop.SetValue(obj, reader[item.Column]); } } catch (Exception e) { e.Data.Add("PropertyName", item.Property); e.Data.Add("ColumnName", item.Column); throw e; } }
private void MappingEnumProperty(DataRow row, object obj, System.Reflection.PropertyInfo[] props, ResultMappingItem item, string enumTypeInfo) { try { var enumType = ORMAssemblyContainer.GetInstance().GetObjectType(enumTypeInfo); if (enumType == null) { throw new Exception("Cannot find type: " + enumTypeInfo); } var prop = props.FirstOrDefault(i => string.Equals(i.Name, item.Property, StringComparison.OrdinalIgnoreCase)); if (row[item.Column] != DBNull.Value) { var enumValue = Enum.Parse(enumType, Convert.ToString(row[item.Column])); prop.SetValue(obj, enumValue); } } catch (Exception e) { e.Data.Add("PropertyName", item.Property); e.Data.Add("ColumnName", item.Column); e.Data.Add("EnumTypeInfo", enumTypeInfo); throw e; } }
private void MappingVirtualProperty(DomainObjectElement element, object obj, System.Reflection.PropertyInfo[] props, ResultMappingItem item) { try { var prop = props.FirstOrDefault(i => string.Equals(i.Name, item.Property, StringComparison.OrdinalIgnoreCase)); prop.SetValue(obj, element.DefaultValue); } catch (Exception e) { e.Data.Add("PropertyName", item.Property); e.Data.Add("ColumnName", item.Column); throw e; } }
private void MappingComplexProperty(IDataReader reader, object obj, System.Reflection.PropertyInfo[] props, ResultMappingItem item, Association asso) { if (asso == null) { throw new Exception("Cannot find Association :" + item.Property); } try { var prop = props.FirstOrDefault(i => string.Equals(i.Name, item.Property, StringComparison.OrdinalIgnoreCase)); } catch (Exception e) { e.Data.Add("PropertyName", item.Property); e.Data.Add("ColumnName", item.Column); throw e; } }
public List <object> GetSubObjects(QueryResultSet resultSet, NSharding.DomainModel.Spi.DomainModel model, DomainObject domainObject, ResultMappingItem mappingItem) { var result = new List <object>(); var type = ORMAssemblyContainer.GetInstance().GetObjectType(domainObject.ClazzReflectType); var props = type.GetProperties(); var resultMapping = mappingItem.ResultMapping; var dataTable = resultSet.GetDataTable(domainObject.DataObjectID); for (int r = 0; r < dataTable.Rows.Count; r++) { var row = dataTable.Rows[r]; var obj = ORMAssemblyContainer.GetInstance().CreateInstance(domainObject.ClazzReflectType); foreach (var item in resultMapping.MappingItems) { switch (item.ItemType) { case ResultMappingItemType.Normal: MappingCommonProperty(row, obj, props, item, domainObject); break; case ResultMappingItemType.Enum: var element = domainObject.Elements.FirstOrDefault(i => i.PropertyName == item.Property); MappingEnumProperty(row, obj, props, item, element.PropertyType); break; case ResultMappingItemType.Virtual: var virtualElement = domainObject.Elements.FirstOrDefault(i => i.PropertyName == item.Property); MappingVirtualProperty(virtualElement, obj, props, item); break; } result.Add(obj); } } return(result); }
public object GetForeignObjects(DataRow row, NSharding.DomainModel.Spi.DomainModel model, ResultMappingItem mappingItem, DomainObject domainObject, Association forAssociation) { var type = ORMAssemblyContainer.GetInstance().GetObjectType(forAssociation.AssoDomainObject.ClazzReflectType); var obj = ORMAssemblyContainer.GetInstance().CreateInstance(forAssociation.AssoDomainObject.ClazzReflectType); var props = type.GetProperties(); foreach (var item in forAssociation.Items) { var targetElement = domainObject.Elements.FirstOrDefault(i => i.ID == item.TargetElementID); MappingCommonProperty(row, obj, props, mappingItem.ResultMapping.MappingItems.FirstOrDefault(i => i.Column == targetElement.Alias), domainObject); } foreach (var refElement in forAssociation.RefElements) { var targetElement = domainObject.Elements.FirstOrDefault(i => i.ID == refElement.ElementID); MappingCommonProperty(row, obj, props, mappingItem.ResultMapping.MappingItems.FirstOrDefault(i => i.Column == targetElement.Alias), domainObject); } return(obj); }
private void MappingCommonProperty(DataRow row, object obj, System.Reflection.PropertyInfo[] props, ResultMappingItem item, DomainObject domainObject) { try { var prop = props.FirstOrDefault(i => string.Equals(i.Name, item.Property, StringComparison.OrdinalIgnoreCase)); if (prop == null) { throw new ArgumentNullException("Prop:" + item.Property); } if (row[item.Column] != DBNull.Value) { var realValue = row[item.Column]; if (prop.PropertyType == typeof(Boolean)) { if (row[item.Column].GetType() == typeof(int)) { realValue = Convert.ToBoolean(Convert.ToInt32(realValue)); } } prop.SetValue(obj, realValue); } } catch (Exception e) { e.Data.Add("PropertyName", item.Property); e.Data.Add("ColumnName", item.Column); throw e; } }