Following on from these posts;
ADO.NET Data Services - Exposing Arbitrary Data (1)
ADO.NET Data Services - Exposing Arbitrary Data (2)
ADO.NET Data Services - Exposing Arbitrary Data (3)
it's time to look at updates.
Using the Entity Framework based service I can write a simple update such as;
demoEntities efService = new demoEntities("http://localhost:32767/WebSite6/EFService.svc");
efService.MergeOption = Microsoft.Data.WebClient.MergeOption.AppendOnly;
author lee = (from a in efService.author
where a.lastName == "Lee"
select a).Single();
lee.firstName = "Still Harper";
efService.UpdateObject(lee);
efService.SaveChanges();
and, using my own in-memory-based service I can write pretty much the same code;
BookContext memService = new BookContext("http://localhost:32767/WebSite6/MemoryService.svc");
memService.MergeOption = Microsoft.Data.WebClient.MergeOption.AppendOnly;
Author lee = (from a in memService.Authors
where a.LastName == "Lee"
select a).Single();
lee.FirstName = "Still Harper";
memService.UpdateObject(lee);
memService.SaveChanges();
and that causes a server side call to Attach and then the framework does the modification and then there's a call to SaveChanges so everything is already in place for that.
If we go for a more complicated update and move a book from one author to another then I can perhaps do;
demoEntities efService = new demoEntities("http://localhost:32767/WebSite6/EFService.svc");
efService.MergeOption = Microsoft.Data.WebClient.MergeOption.AppendOnly;
author lee = (from a in efService.author
where a.lastName == "Lee"
select a).Single();
author dickens = (from a in efService.author
where a.lastName == "Dickens"
select a).Single();
efService.LoadProperty(lee, "book");
book b = lee.book[0];
efService.DeleteBinding(lee, "book", b);
efService.AddBinding(dickens, "book", b);
efService.SaveChanges();
and that seems to work ok for me. If I want to do that with my in-memory data then I can do;
BookContext memService = new BookContext("http://localhost:32767/WebSite6/MemoryService.svc");
memService.MergeOption = Microsoft.Data.WebClient.MergeOption.AppendOnly;
Author lee = (from a in memService.Authors
where a.LastName == "Lee"
select a).Single();
Author dickens = (from a in memService.Authors
where a.LastName == "Dickens"
select a).Single();
memService.LoadProperty(lee, "Books");
Book b = lee.Books[0];
memService.DeleteBinding(lee, "Books", b);
memService.DeleteBinding(b, "Author", lee);
memService.AddBinding(dickens, "Books", b);
memService.AddBinding(b, "Author", dickens);
memService.SaveChanges();
and that seems to work ok (I emphasise seems because perhaps there's a false-positive effect coming from the combination of the way in which I wrote the server-side code with the client-side code).
With my in-memory service, it's not quite clear to me how I should respond to DeleteBinding from the client. What seems to happen is that I get calls to Attach and then I think the framework just modifies the Book and Author propeties directly itself. I'm not sure exactly whether I should write my Books collection such that when a Book is removed from its Author the Author property on that Book is nulled out or whether I leave it like it is here where the client has to call DeleteBinding explicitly to make that happen.
The other thing I'd say is that there are still two methods on IUpdatable that I haven't implemented, namely;
SetReferenceProperty
AttachResourceToCollection
and, as yet, I'm not entirely sure what they're used for :-) as I thought I'd encounter them doing the inserting, updating, deleting that I've done so far across these 4 posts.
At the end of the these 4 posts I think there's a need for a document around IUpdatable that provides the definitive view of what each method is meant to do, what it means for the entity classes that you return (e.g. who is responsible for ensuring that changes to an entity are atomic? presumably the framework?) and the collection classes that you return (e.g. who is responsible for ensuring thread-safety? presumably the implementor of IUpdatable?).
If I find such a doc, I'll update the post.
Posted
Thu, Jan 3 2008 4:00 AM
by
mtaulty