0

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?

Dennis
  • 37,026
  • 10
  • 82
  • 150

1 Answers1

1

You could probably use Detach to undo changes. See this post: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/a944a15f-4a2f-424a-8222-584aad5eaf73

Pawel
  • 31,342
  • 4
  • 73
  • 104
  • Sorry, I should mention, that undo operations is implemented in tiny framework, which I don't want to couple with data services. So, I need pure collection-based solution. Moreover, this look natural - if user adds deleted item, than he wants to restore it back. It's OK to work with data service context in `DataServiceCollection` callbacks, but not in the undo code. – Dennis Oct 22 '12 at 17:16