public void Delete(IEnumerable <DataSourceId> dataSourceIds) { Verify.ArgumentNotNull(dataSourceIds, "dataSourceIds"); CheckTransactionNotInAbortedState(); using (XmlDataProviderDocumentCache.CreateEditingContext()) { var validated = new Dictionary <DataSourceId, FileRecord>(); // verify phase foreach (DataSourceId dataSourceId in dataSourceIds) { Verify.ArgumentCondition(dataSourceId != null, nameof(dataSourceIds), "The enumeration may not contain null values"); XmlDataTypeStore dataTypeStore = _xmlDataTypeStoresContainer.GetDataTypeStore(dataSourceId.InterfaceType); var dataScope = dataSourceId.DataScopeIdentifier; var culture = dataSourceId.LocaleScope; var type = dataSourceId.InterfaceType; var dataId = dataSourceId.DataId; var dataTypeStoreScope = dataTypeStore.GetDataScope(dataScope, culture, type); if (dataTypeStore.Helper._DataIdType != dataId.GetType()) { throw new ArgumentException("Only data ids from this provider is allowed to be deleted on on the provider"); } var fileRecord = GetFileRecord(dataTypeStore, dataTypeStoreScope); var index = fileRecord.RecordSet.Index; if (!index.ContainsKey(dataId)) { throw new ArgumentException($"Cannot delete a data item, no data element corresponds to the given data id: '{dataId.Serialize(null)}'; Type: '{type}'; DataScope: '{dataScope}'; Culture: '{culture}'", nameof(dataSourceIds)); } validated.Add(dataSourceId, fileRecord); } // commit phase foreach (var dataSourceId in validated.Keys) { FileRecord fileRecord = validated[dataSourceId]; fileRecord.RecordSet.Index.Remove(dataSourceId.DataId); fileRecord.Dirty = true; } XmlDataProviderDocumentCache.SaveChanges(); SubscribeToTransactionRollbackEvent(); } }
public void Update(IEnumerable <IData> dataset) { Verify.ArgumentNotNull(dataset, "dataset"); CheckTransactionNotInAbortedState(); using (XmlDataProviderDocumentCache.CreateEditingContext()) { var validatedFileRecords = new Dictionary <DataSourceId, FileRecord>(); var validatedElements = new Dictionary <DataSourceId, XElement>(); // verify phase foreach (IData wrappedData in dataset) { var data = DataWrappingFacade.UnWrap(wrappedData); Verify.ArgumentCondition(data != null, "dataset", "Collection contains a null element."); Verify.ArgumentCondition(data.DataSourceId != null, "dataset", "Collection contains a data item with DataSourceId null property."); ValidationHelper.Validate(data); XmlDataTypeStore dataTypeStore = _xmlDataTypeStoresContainer.GetDataTypeStore(data.DataSourceId.InterfaceType); var dataScope = data.DataSourceId.DataScopeIdentifier; var culture = data.DataSourceId.LocaleScope; var type = data.DataSourceId.InterfaceType; var dataTypeStoreScope = dataTypeStore.GetDataScope(dataScope, culture, type); dataTypeStore.Helper.ValidateDataType(data); if (null == data.DataSourceId) { throw new ArgumentException("The DataSourceId property of the data argument must not be null", "data"); } if (data.DataSourceId.ProviderName != _dataProviderContext.ProviderName) { throw new ArgumentException("The data element does not belong to this provider", "data"); } var fileRecord = GetFileRecord(dataTypeStore, dataTypeStoreScope); var index = fileRecord.RecordSet.Index; IDataId dataId = data.DataSourceId.DataId; XElement element = index[dataId]; Verify.ArgumentCondition(element != null, "dataset", "No data element corresponds to the given data id"); IXElementWrapper wrapper = data as IXElementWrapper; Verify.ArgumentCondition(wrapper != null, "dataset", "The type of data was expected to be of type {0}".FormatWith(typeof(IXElementWrapper))); XElement updatedElement = CreateUpdatedXElement(wrapper, element); validatedFileRecords.Add(data.DataSourceId, fileRecord); validatedElements.Add(data.DataSourceId, updatedElement); } foreach (var key in validatedElements.Keys) { FileRecord fileRecord = validatedFileRecords[key]; fileRecord.Dirty = true; fileRecord.RecordSet.Index[key.DataId] = validatedElements[key]; } XmlDataProviderDocumentCache.SaveChanges(); SubscribeToTransactionRollbackEvent(); } }