/// <summary> /// Handles the save result. /// </summary> /// <param name="result">The result</param> /// <returns></returns> private static bool HandleSaveResult(SaveResult result) { switch (result) { case SaveResult.Skipped: return(false); case SaveResult.Success: return(true); case SaveResult.Conflict: throw new UpdateConflictException("A synchronization conflict was detected during the update"); case SaveResult.Fail: throw new SaveFailedException("The save appears to have failed for an unknown reason, or did not affect the expected number of records"); default: throw new ArgumentException("Unrecognized or invalid SaveResult"); } }
/// <summary> /// Saves the collection. /// </summary> /// <param name="collection">The collection.</param> /// <param name="criteria">The criteria of type <see cref="Zonkey.UpdateCriteria"/>.</param> /// <param name="affect">The <see cref="Zonkey.UpdateAffect"/> value that determines which rows to affect.</param> /// <param name="selectBack">The <see cref="Zonkey.SelectBack"/> value that determines whether to select back the changed rows.</param> /// <param name="continueOnError">if set to <c>true</c> continues past errors.</param> /// <returns> /// A value of type <see cref="Zonkey.CollectionSaveResult{T}"/> /// </returns> public CollectionSaveResult <T> TrySaveCollection(ICollection <T> collection, UpdateCriteria criteria, UpdateAffect affect, SelectBack selectBack, bool continueOnError) { var colResult = new CollectionSaveResult <T>(); var bindCol = collection as ITrackDeletedItems <T>; if (bindCol != null) { foreach (T obj in bindCol.DeletedItems) { var objSV = obj as ISavable; if (objSV != null) { if (objSV.DataRowState == DataRowState.Deleted) { try { DeleteItem(obj); } catch (Exception ex) { colResult.Exceptions.Add(new CollectionSaveExceptionItem <T>(obj, ex)); if (!continueOnError) { throw; } } colResult.Deleted.Add(obj); } else { colResult.Skipped.Add(obj); } } else { colResult.Failed.Add(obj); } } } foreach (T obj in collection) { var objSV = obj as ISavable; if (objSV != null) { if (objSV.DataRowState != DataRowState.Unchanged) { try { SaveResult saveResult = TrySave(obj, criteria, affect, selectBack); switch (saveResult.Status) { case SaveResultStatus.Skipped: colResult.Skipped.Add(obj); break; case SaveResultStatus.Conflict: colResult.Conflicted.Add(obj); break; case SaveResultStatus.Fail: colResult.Failed.Add(obj); break; case SaveResultStatus.Success: if (saveResult.SaveType == SaveType.Insert) { colResult.Inserted.Add(obj); } else { colResult.Updated.Add(obj); } break; } } catch (Exception ex) { colResult.Exceptions.Add(new CollectionSaveExceptionItem <T>(obj, ex)); if (!continueOnError) { throw; } } } else { colResult.Skipped.Add(obj); } } else { colResult.Failed.Add(obj); } } return(colResult); }