private void InsertMembers(DomainObject root, DomainObject parent, DomainObject current, PropertyRepositoryAttribute tip) { var objs = current.GetValue(tip.Property) as IEnumerable; DataTable middle = null; foreach (DomainObject obj in objs) { if (obj.IsEmpty()) { continue; } var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType); if (child.Type == DataTableType.ValueObject) { //我们需要为ValueObject补充编号 (obj as IValueObject).TrySetId(Guid.NewGuid()); } child.InsertMember(root, current, obj); if (middle == null) { middle = child.Middle; } } if (middle != null) { middle.InsertMiddle(root, current, objs); } }
public override void OnInserted(DomainObject obj, DataTable table) { DataContext.Current.OpenLock(QueryLevel.Single); var id = (obj as IEntityObject).GetIdentity(); var parent = obj.GetValue <IEntityObject>(this.ParentProperty); if (SqlContext.GetDbType() == DatabaseType.SQLServer) { if (parent.IsEmpty()) { //自身为根 DataPortal.Direct(obj.ObjectType, (conn) => { var sql = string.Format("update dbo.{0} set lft=@lft,rgt=@rgt,rootId=@id where id=@id;", table.Name); conn.Execute(sql, new { lft = 0, rgt = 1, id }); }); } else { var parentId = parent.GetIdentity(); var sql = _getUpdateLRForInsertSqlBySqlServer(table.Name); //显示开启锁 DataPortal.Direct(obj.ObjectType, (conn) => { conn.Execute(sql, new { id, parentId }); }); } return; } throw new MapperNotImplementedException(obj.ObjectType, "insert"); }
/// <summary> /// 根据当前属性值,删除成员 /// </summary> /// <param name="root"></param> /// <param name="parent"></param> /// <param name="current"></param> /// <param name="tip"></param> private void DeleteMemberByPropertyValue(DomainObject root, DomainObject parent, DomainObject current, PropertyRepositoryAttribute tip) { var obj = current.GetValue(tip.Property) as DomainObject; if (!obj.IsEmpty()) { var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType); child.DeleteMember(root, parent, obj); } }
/// <summary> /// 获取基元类型的属性值 /// </summary> /// <returns></returns> private static object GetPrimitivePropertyValue(DomainObject obj, PropertyRepositoryAttribute tip) { var value = obj.GetValue(tip.Property); if (!tip.IsEmptyable) { return(value); } var e = (IEmptyable)value; return(e.IsEmpty() ? null : e.GetValue()); //可以存null值在数据库 }
/// <summary> /// 修改current对应的集合属性 /// </summary> /// <param name="root"></param> /// <param name="parent"></param> /// <param name="current"></param> /// <param name="tip"></param> private void UpdateMembers(DomainObject root, DomainObject parent, DomainObject current, PropertyRepositoryAttribute tip) { var objs = current.GetValue(tip.Property) as IEnumerable; foreach (DomainObject obj in objs) { if (!obj.IsEmpty()) { var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType); //方法内部会检查是否为脏,为脏的才更新 child.UpdateMember(root, current, obj); } } }
private void InsertAndCollectValueObject(DomainObject root, DomainObject parent, DomainObject current, PropertyRepositoryAttribute tip, DynamicData data) { var field = GetQuoteField(this, tip.PropertyName); var obj = current.GetValue(tip.Property) as DomainObject; if (obj.IsEmpty()) { data.Add(field.Name, Guid.Empty); } else { (obj as IValueObject).TrySetId(Guid.NewGuid()); var id = GetObjectId(obj); data.Add(field.Name, id); //保存数据 var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType); child.InsertMember(root, current, obj); } }
/// <summary> /// 根据对象当前定义的属性,删除成员 /// </summary> /// <param name="root"></param> /// <param name="parent"></param> /// <param name="current"></param> /// <param name="tip"></param> private void DeleteMembersByPropertyValue(DomainObject root, DomainObject parent, DomainObject current, PropertyRepositoryAttribute tip) { var objs = current.GetValue(tip.Property) as IEnumerable; DeleteMembers(root, parent, current, objs, tip); }
private void InsertAndCollectValue(DomainObject root, DomainObject parent, DomainObject current, PropertyRepositoryAttribute tip, DynamicData data) { if (this.IsSnapshot && !tip.Snapshot) { return; //如果是快照,那么需要过滤不参与快照的属性 } //if (tip.TrySaveData(this.ObjectType, current, data)) return; switch (tip.DomainPropertyType) { case DomainPropertyType.Primitive: { var value = GetPrimitivePropertyValue(current, tip); data.Add(tip.PropertyName, value); } break; case DomainPropertyType.PrimitiveList: { var value = current.GetValue(tip.Property); //仅存中间表 var values = GetValueListData(value); var child = GetChildTableByRuntime(this, tip); //无论是派生还是基类,基础表对应的中间表都一样 child.InsertMiddle(root, current, values); } break; case DomainPropertyType.ValueObject: { InsertAndCollectValueObject(root, parent, current, tip, data); } break; case DomainPropertyType.AggregateRoot: { var field = GetQuoteField(this, tip.PropertyName); object obj = current.GetValue(tip.Property); var id = GetObjectId(obj); data.Add(field.Name, id); } break; case DomainPropertyType.EntityObject: { var obj = current.GetValue(tip.Property) as DomainObject; var id = GetObjectId(obj); var field = GetQuoteField(this, tip.PropertyName); data.Add(field.Name, id); //收集外键 //保存引用数据 if (!obj.IsEmpty()) { var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType); child.InsertMember(root, current, obj); } } break; case DomainPropertyType.AggregateRootList: { //仅存中间表 var objs = current.GetValue(tip.Property) as IEnumerable; var child = GetChildTableByRuntime(this, tip); //无论是派生还是基类,基础表对应的中间表都一样 child.Middle.InsertMiddle(root, current, objs); } break; case DomainPropertyType.ValueObjectList: case DomainPropertyType.EntityObjectList: { InsertMembers(root, parent, current, tip); } break; } }
/// <summary> /// /// </summary> /// <param name="root"></param> /// <param name="parent"></param> /// <param name="current"></param> /// <param name="tip"></param> /// <param name="data"></param> /// <returns>当内部成员发生变化,返回true</returns> private bool UpdateAndCollectChangedValue(DomainObject root, DomainObject parent, DomainObject current, PropertyRepositoryAttribute tip, DynamicData data) { switch (tip.DomainPropertyType) { case DomainPropertyType.Primitive: { if (current.IsPropertyChanged(tip.Property)) { var value = GetPrimitivePropertyValue(current, tip); data.Add(tip.PropertyName, value); return(true); } } break; case DomainPropertyType.PrimitiveList: { if (current.IsPropertyChanged(tip.Property)) { //删除老数据 var child = GetChildTableByRuntime(this, tip); child.DeleteMiddleByMaster(root, current); var value = current.GetValue(tip.Property); //仅存中间表 var values = GetValueListData(value); child.InsertMiddle(root, current, values); return(true); } } break; //case DomainPropertyType.ValueObject: // { // if (current.IsPropertyChanged(tip.Property)) // { // //删除原始数据 // DeleteMemberByOriginalData(root, parent, current, tip); // //新增数据 // InsertAndCollectValueObject(root, parent, current, tip, data); // return true; // } // } // break; case DomainPropertyType.AggregateRoot: { if (current.IsPropertyChanged(tip.Property)) { var field = GetQuoteField(this, tip.PropertyName); object obj = current.GetValue(tip.Property); var id = GetObjectId(obj); data.Add(field.Name, id); return(true); } } break; case DomainPropertyType.ValueObject: //虽然值对象的成员不会变,但是成员的成员也许会改变 case DomainPropertyType.EntityObject: { if (current.IsPropertyChanged(tip.Property)) { var obj = current.GetValue(tip.Property) as DomainObject; if (tip.DomainPropertyType == DomainPropertyType.ValueObject) { (obj as IValueObject).TrySetId(Guid.NewGuid()); } var id = GetObjectId(obj); var field = GetQuoteField(this, tip.PropertyName); data.Add(field.Name, id); //收集外键 //删除原始数据 DeleteMemberByOriginalData(root, parent, current, tip); //保存引用数据 if (!obj.IsEmpty()) { var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType); child.InsertMember(root, current, obj); } return(true); } else if (current.IsPropertyDirty(tip.Property)) { //如果引用的内聚成员是脏对象,那么需要修改 var obj = current.GetValue(tip.Property) as DomainObject; if (!obj.IsEmpty()) { //从衍生表中找到对象表 var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType); child.UpdateMember(root, parent, obj); } return(true); } } break; case DomainPropertyType.AggregateRootList: { if (current.IsPropertyChanged(tip.Property)) { //删除老数据 var child = GetChildTableByRuntime(this, tip); child.Middle.DeleteMiddleByMaster(root, current); //追加新数据 var objs = current.GetValue(tip.Property) as IEnumerable; child.Middle.InsertMiddle(root, current, objs); return(true); } } break; case DomainPropertyType.ValueObjectList: case DomainPropertyType.EntityObjectList: { if (current.IsPropertyChanged(tip.Property)) { //引用关系发生了变化,删除重新追加 //这里要注意,需要删除的是数据库的数据,所以要重新读取 //删除原始数据 DeleteMembersByOriginalData(root, parent, current, tip); //加入新数据 InsertMembers(root, parent, current, tip); return(true); } else if (current.IsPropertyDirty(tip.Property)) { //引用关系没变,只是数据脏了 UpdateMembers(root, parent, current, tip); return(true); } } break; } return(false); }