/// <summary> /// The add. /// </summary> /// <param name="item"> /// The item. /// </param> public bool Add(SyncHelperItem item) { throw new NotImplementedException(); if (item == null) { throw new ArgumentNullException("item"); } lock (this.items) { this.AddWithoutLock(item); } }
/// <summary> /// The add without lock. /// </summary> /// <param name="item"> /// The item. /// </param> /// <exception cref="ArgumentNullException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> private void AddWithoutLock(SyncHelperItem item) { if (item == null) { throw new ArgumentNullException("item"); } var existingItem = this.items.SingleOrDefault(x => x.Entity == item.Entity); if (existingItem != null) { const int DeleteAction = 2; const int CreateAction = 0; const int EditAction = 1; var oldAction = existingItem.Delete ? DeleteAction : existingItem.Entity.StoreId == default(int) ? CreateAction : EditAction; var newAction = item.Delete ? DeleteAction : item.Entity.StoreId == default(int) ? CreateAction : EditAction; // Delete, Edit if (oldAction == DeleteAction && newAction == EditAction) { throw new InvalidOperationException("Cannot edit an entity that is pending deletion."); } // Delete, Create if (oldAction == DeleteAction && newAction == CreateAction) { throw new InvalidOperationException("Cannot create an entity that is pending creation."); } // Delete, Delete if (oldAction == DeleteAction && newAction == DeleteAction) { throw new InvalidOperationException("Cannot delete an entity that is pending deletion."); } // Edit, Create if (oldAction == EditAction && newAction == CreateAction) { throw new InvalidOperationException("Cannot create an entity that is pending creation."); } // Edit, Edit // No action is required as the entity is already queued. if (oldAction == EditAction && newAction == EditAction) { return; } // Edit, Delete if (oldAction == EditAction && newAction == DeleteAction) { Debug.WriteLine("An entity that is marked for edit will now be marked for deletion instead."); existingItem.Delete = true; } // Create, Create if (oldAction == CreateAction && newAction == CreateAction) { // The case is where the user edits an item that is newly created. It just means that the entity // has not yet received a store id. Debug.WriteLine("An edit was performed on an entity that is marked for creation."); return; } // Create, Edit if (oldAction == CreateAction && newAction == EditAction) { throw new InvalidOperationException( "An entity marked for creation was edited. This should not happen."); } // Create, Delete if (oldAction == CreateAction && newAction == DeleteAction) { Debug.WriteLine("An entity that is queued for creation was deleted."); this.items.Remove(existingItem); return; } } else { this.items.Add(item); } }