Skip to main content
Home Forums Silverlight Programming Accessing Web Services with Silverlight ObservableCollection-over-WCF-to-Silverlight 3-and-back
3 replies. Latest Post by mrjvdveen on November 5, 2009.
(0)
NWA210
Member
8 points
15 Posts
11-05-2009 2:34 AM |
I'm wondering if you can explain an ObservableCollection-over-WCF-to-Silverlight 3 question. I've been trolling the Internet for days and I'm running out of time to figure this out.
Suppose you have an ObservableCollection of POCOs that inherit from INotifyPropertyChanged like this guy has http://weblogs.asp.net/joelvarty/archive/2008/11/17/silverlight-databinding-the-observable-collection.aspx
Now suppose you return his UserCollection to a Silverlight client over WCF and the client puts the collection in a DataGrid.ItemSource. Now someone modifies a name in one of the FirstName cells of the grid.
How do you get just that changed cell or row of that cell to go back to the middle tier? Or if you send the whole UserCollection back to the middle tier how can you tell what's changed in the collection?
In my case since I cannot figure out how to send just the changed rows back the middle tier, I created an [OperationContract] method in my WCF service which takes the whole collection:
SaveChanges(ObservableCollection<Person> person)
and in my Silverlight client I have a button click event call the method
svc.SaveChanges(datagrid.ItemSource)
When the WCF method receives the collection, I can see the modified name if I inspect each row however I have no idea how to programmatically know that the name has been modified in any given row. - the same problem I had in the Silverlight client, of course. Where does INotifyPropertyChanged fit into this scenario?
Ultimately, I need to transfer the changed information from the POCOs to EntityFramework objects so that the change makes it back to the database.
mrjvdveen
Participant
1939 points
367 Posts
11-05-2009 2:42 AM |
You'd have to record the changes as they happen, indeed by using INotifyPropertyChanged. You could record the changes in a seperate instance of the same class and compare each property, or you could write a simple class that has an Id and a set of changes, consisting of a fieldname and it's new value. Send that instance to the service and you know what to update.
I know it's a hassle and it's exactly the reason why our team don't use POCOs. We use a set of base classes that handles all this stuff for us, including a large part of the save.
HTH.
11-05-2009 3:13 AM |
I'm not sure how to subscribe to the PropertyChanged event in my Silverlight code.
11-05-2009 3:42 AM |
You'd have to take each instance and attach to the event like this:
myPoco.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(myPoco_PropertyChanged);