I'm looking at WCF Data Services, particularly at change tracking implementation in DataServiceCollection
.
Let's consider an undo scenario in application, which manages blogs and blog entries (each blog has many blog entries). I'm testing two sorts of undo operations:
- new blog entry was added (undo operation is to remove entry);
- existing blog entry was deleted (undo operation is to add entry).
Here's code sample:
var context = new BloggingContext(new Uri("http://localhost/WCFDataServicesDemo/"));
var blogs = new DataServiceCollection<Blog>(context.Blogs.Expand(b => b.Entries), TrackingMode.AutoChangeTracking);
var blog = blogs.Where(b => b.Id == 1).Single();
// new entry
var newEntry = new BlogEntry
{
// some blog entry's properties
};
// add new entry
blog.Entries.Add(newEntry);
// undo add
blog.Entries.Remove(newEntry);
// existing entry
var existingEntry = blog.Entries[0];
// remove existing entry
blog.Entries.Remove(existingEntry);
// undo remove
blog.Entries.Add(existingEntry);
context.SaveChanges();
The first undo operation (add new entry, then remove it on undo) works fine.
The second undo operation (remove existing entry, then add it on undo) works asymmetrically.
This line:
blog.Entries.Remove(existingEntry);
marks corresponding entity as Deleted
in context's change tracker, which is right. But this line:
blog.Entries.Add(existingEntry);
does nothing with change tracker (existingEntry
remains in Deleted
state), but, of course, adds item into collection.
Hence, when SaveChanges
is called, this entry being deleted from database, but it remains in the collection at client side.
I've tried to handle this undo manually, by setting callbacks in DataServiceCollection
constructor, but here I'm getting the same asymmetric behavior - collectionChangedCallback
doesn't fire, when I'm trying to add item, which is marked as Deleted
.
What am I doing wrong?
How to "restore" deleted item?