/// <summary> /// 更新数据项 /// </summary> /// <param name="dataItemEntity"></param> public void UpdateDataItem(DataItemEntityDev dataItemEntity) { if (dataItemEntity == null || dataItemEntity.Owner == null) { Debug.Assert(false, "参数异常"); return; } string dataEntityId = dataItemEntity.Owner.Id; //判断指定的数据实体是否存在 bool entityExist = EntityExistById(dataEntityId); Debug.Assert(entityExist, "数据实体不存在"); if (entityExist == false) { return; } //拿数据实体文件 string file = Path.Combine(Constant.PACKAGE_DATAENTITY_FOLDER, dataEntityId); //在数据实体XML中更新数据项节点 //尝试从缓存中获取 XElement entityElement = (XElement)_cachingService.GetData(dataEntityId); if (entityElement == null) { bool fileExist = _packageService.Current.Container(file); Debug.Assert(fileExist, "数据实体文件不存在"); if (fileExist == false) { return; } string strEntity = _packageService.Current.GetFileContent(file); entityElement = XElement.Parse(strEntity); _cachingService.Add(dataEntityId, entityElement); } //直接更新缓存中的对象,无需更新缓存了 entityElement.XPathSelectElement(String.Format(XPATH_DataEntity_SelectItem, dataItemEntity.Id)).ReplaceWith(XElement.Parse(dataItemEntity.ToXml())); //保存文件 _packageService.Current.AddFileContent(entityElement.ToString(), file); //发布事件 DataItemEntityEventArgs args = new DataItemEntityEventArgs(dataItemEntity); _eventAggregator.GetEvent <DataItemEntityUpdatedEvent>().Publish(args); }
/// <summary> /// 添加一个数据项 /// </summary> /// <param name="dataItemEntity"></param> /// <param name="parentId"></param> public void AddDataItem(DataItemEntityDev dataItemEntity, string dataEntityId) { if (dataItemEntity == null || String.IsNullOrEmpty(dataEntityId)) { Debug.Assert(false, "参数异常"); return; } //判断指定的数据实体是否存在 bool entityExist = EntityExistById(dataEntityId); Debug.Assert(entityExist, "数据实体不存在"); if (entityExist == false) { return; } //拿数据实体文件 string file = Path.Combine(Constant.PACKAGE_DATAENTITY_FOLDER, dataEntityId); //在数据实体XML中加上数据项节点 //尝试从缓存中获取 XElement entityElement = (XElement)_cachingService.GetData(dataEntityId); if (entityElement == null) { bool fileExist = _packageService.Current.Container(file); Debug.Assert(fileExist, "数据实体文件不存在"); if (fileExist == false) { return; } string strEntity = _packageService.Current.GetFileContent(file); entityElement = XElement.Parse(strEntity); _cachingService.Add(dataEntityId, entityElement); } //这里直接操作了缓存中的XElement,无需更新缓存了 entityElement.XPathSelectElement(XPATH_DataEntity_Items).Add(XElement.Parse(dataItemEntity.ToXml())); //保存文件 _packageService.Current.AddFileContent(entityElement.ToString(), file); //发布事件 DataItemEntityEventArgs args = new DataItemEntityEventArgs(dataItemEntity); _eventAggregator.GetEvent <DataItemEntityAddedEvent>().Publish(args); }