/// <summary> /// 把整个聚合对象的 Id 设置完整。 /// </summary> /// <param name="oldEntity"></param> /// <param name="newEntity"></param> private static void MergeIdRecur(Entity oldEntity, Entity newEntity) { oldEntity.LoadProperty(Entity.IdProperty, newEntity.GetProperty(Entity.IdProperty)); foreach (var field in oldEntity.GetLoadedChildren()) { var listProperty = field.Property as IListProperty; if (listProperty != null) { var oldChildren = field.Value as EntityList; var newChildren = newEntity.GetLazyList(listProperty) as EntityList; //两个集合应该是一样的数据、顺序?如果后期出现 bug,则修改此处的逻辑为查找到对应项再修改。 for (int i = 0, c = oldChildren.Count; i < c; i++) { MergeIdRecur(oldChildren[i], newChildren[i]); } //同步组合父对象 Id oldChildren.SyncParentEntityId(oldEntity); //同步 TreePId if (oldChildren.SupportTree) { for (int i = 0, c = oldChildren.Count; i < c; i++) { var oldChild = oldChildren[i]; var newChild = newChildren[i]; oldChild.TreePId = newChild.TreePId; } } } } }
/// <summary> /// 迭归检测一个组合实体中是否有新添加的实体 /// </summary> /// <param name="entity"></param> /// <returns></returns> private static bool HasNewEntity(Entity entity) { if (entity.IsNew) { return(true); } var enumerator = entity.GetLoadedChildren(); while (enumerator.MoveNext()) { var field = enumerator.Current.Value; var list = field as EntityList; if (list != null) { foreach (var child in list) { if (HasNewEntity(child)) { return(true); } } } else { var child = field as Entity; if (HasNewEntity(child)) { return(true); } } } return(false); }
/// <summary> /// 提交更新指定实体的组合子列表。 /// </summary> protected virtual void SubmitChildren(Entity entity) { var enumerator = entity.GetLoadedChildren(); while (enumerator.MoveNext()) { var child = enumerator.Current.Value; SaveRecur(child, entity); } }
/// <summary> /// 提交更新指定实体的组合子列表。 /// </summary> protected virtual void SubmitChildren(Entity entity) { var enumerator = entity.GetLoadedChildren(); while (enumerator.MoveNext()) { var child = enumerator.Current.Value; //使用组合子自己的仓库来进行提交。 var repo = child.GetRepository() as EntityRepository; var repoData = repo.RdbDataProvider; repoData.SubmitChildrenComponent(child, entity); } }
/// <summary> /// 删除所有组合子。 /// /// 子类重写此方法来实现新的删除逻辑。 /// 注意,此方法只会在指定了 <see cref="EnableDeletingChildrenInMemory"/> 时,才会调用。 /// </summary> protected virtual void DeleteChildren(Entity entity) { _repository.LoadAllChildren(entity); foreach (var childField in entity.GetLoadedChildren()) { var children = childField.Value as EntityList; if (children != null && children.Count > 0) { //删除所有子。 children.Clear(); SaveRecur(children, entity); } } }
/// <summary> /// 迭归检测一个组合实体中是否有新添加的实体 /// </summary> /// <param name="entity"></param> /// <returns></returns> private static bool HasNewEntity(Entity entity) { if (entity.IsNew) return true; var enumerator = entity.GetLoadedChildren(); while (enumerator.MoveNext()) { var field = enumerator.Current.Value; var list = field as EntityList; if (list != null) { foreach (var child in list) { if (HasNewEntity(child)) return true; } } else { var child = field as Entity; if (HasNewEntity(child)) return true; } } return false; }
/// <summary> /// 提交更新指定实体的组合子列表。 /// </summary> protected virtual void SubmitChildren(Entity entity) { var enumerator = entity.GetLoadedChildren(); while (enumerator.MoveNext()) { var child = enumerator.Current.Value; SaveRecur(child, entity); } }