/// <summary> /// 删除不必要的对象,只留下需要保存的“脏”数据 /// </summary> /// <param name="diffEntity">The difference entity.</param> /// <param name="entityInfo">The entity information.</param> protected virtual void ClearDataCore(Entity diffEntity, EntityMeta entityInfo) { foreach (var item in entityInfo.ChildrenProperties) { var mp = item.ManagedProperty; //如果是懒加载属性,并且没有加载数据时,不需要遍历此属性值 if (!diffEntity.FieldExists(mp)) { continue; } var children = diffEntity.GetProperty(mp) as EntityList; if (children == null) { continue; } for (int i = children.Count - 1; i >= 0; i--) { var child = children[i]; if (!child.IsDirty) { children.Remove(child); children.DeletedList.Remove(child); } else { this.ClearData(child); } } } }
/// <summary> /// 清除子对象集合 /// </summary> /// <param name="oldEntity">The old entity.</param> /// <param name="entityInfo">The entity information.</param> protected virtual void MakeSavedCore(Entity oldEntity, EntityMeta entityInfo) { foreach (var item in entityInfo.ChildrenProperties) { var mp = item.ManagedProperty as IListProperty; //如果是懒加载属性,并且没有加载数据时,不需要遍历此属性值 if (!oldEntity.FieldExists(mp)) { continue; } var children = oldEntity.GetProperty(mp) as EntityList; if (children == null) { continue; } //清除已删除数据 children.CastTo <EntityList>().DeletedList.Clear(); //所有子对象,都标记为已保存 for (int i = children.Count - 1; i >= 0; i--) { var child = children[i] as Entity; if (child.IsDirty || child.IsNew) { MakeSaved(child); } } } oldEntity.MarkSaved(); }
/// <summary> /// 复制目标对象的所有字段。 /// 子类重写此方法额外做以下几件事: /// 1. 如果有自定义字段,请在此方法中进行值拷贝。 /// 2. 如果有延迟加载的外键引用对象 ILazyEntityRef,请调用它的 Clone 方法进行拷贝。 /// 3. 如果使用了快速字段 FastField 来进行属性的缓存,请在基类完成 Clone 后,调用本类的 ResetFastField 方法来清空缓存。 /// </summary> /// <param name="source">The source.</param> /// <param name="options">The options.</param> /// <exception cref="System.ArgumentNullException">source /// or /// options</exception> protected virtual void CloneCore(Entity source, CloneOptions options) { if (source == null) { throw new ArgumentNullException("source"); } if (options == null) { throw new ArgumentNullException("options"); } //准备一些控制变量 var grabChildren = options.HasAction(CloneActions.GrabChildren); var childrenRecur = options.HasAction(CloneActions.ChildrenRecur); var copyId = options.HasAction(CloneActions.IdProperty); var cloneParentRef = options.HasAction(CloneActions.ParentRefEntity); var cloneRef = options.HasAction(CloneActions.RefEntities); var ingoreList = options.RetrieveIgnoreList(false); //如果需要拷贝id,则应该先拷贝id。否则后续在拷贝组合子时,可能访问不当前实体的 Id。 if (copyId) { this.CopyProperty(source, IdProperty, options); } //复制目标对象的所有托管属性。 var allProperties = this.PropertiesContainer.GetAvailableProperties(); for (int i = 0, c = allProperties.Count; i < c; i++) { var property = allProperties[i]; if (property.IsReadOnly) { continue; } //过滤一些不需要拷贝的属性 if (property == IdProperty) { continue; //Id 已经提前处理过了。 } if (ingoreList != null && ingoreList.Contains(property)) { continue; } if (property is IListProperty) { if (childrenRecur || grabChildren) { if (!source.FieldExists(property)) { this.ResetProperty(property); } else { var listProperty = property as IListProperty; var sourceList = source.GetProperty(listProperty) as EntityList; EntityList children = null; if (childrenRecur) { children = this.LoadLazyList(listProperty, true); children.Clone(sourceList, options);//内部 Add 时会调用 children.SetParentEntity(this); } else//grabChildren { children = sourceList; this.LoadProperty(property, children);//Load 时会调用 children.SetParentEntity(this); } } } } else if (property is IRefEntityProperty) { bool copyEntity = (property as IRefEntityProperty).ReferenceType == ReferenceType.Parent ? cloneParentRef : cloneRef; if (copyEntity && source.GetProperty(property) != null) { this.CopyProperty(source, property, options); } } else { this.CopyProperty(source, property, options); } } var supportTree = this.SupportTree; if (supportTree) { this.CloneTreeRelations(source, options, childrenRecur, grabChildren, cloneParentRef); } //如果 Id 值没有拷贝,那么组合子实体中的 PId 需要重新整理。 //由于这种场景下没有拷贝 Id,又需要重新整理 PId 和 TreePId,所以可以使用 SetProperty 方法来变更实体的状态。 if (!copyId && (childrenRecur || grabChildren)) { foreach (var child in this.GetLoadedChildren()) { var list = child.Value as EntityList; if (list != null) { list.SetParentEntity(this); } } if (supportTree) { (this as ITreeComponent).EachNode(e => { e.SyncTreeChildrenPId(); return(false); }); } } //如果是新构建的实体,持久化状态也完全拷贝。(如果 Id 没有被拷贝,说明是只拷贝值,那么不需要变更新实体的状态) if (this.IsNew && copyId && !source.IsNew) { this.PersistenceStatus = source.PersistenceStatus; } options.NotifyCloned(source, this); }