Since the previous part in this series, the final version of Silverlight 3 has been released, and a new version of RIA Services has been released: the July 2009 drop. This release includes quite a few changes, so I had to change my demo source code around quite a bit to make it work with the new release. This post details the changes that have been made – and a new sourcecode download is included as well! 🙂
First of all, in the previous drop, you’d often bind to completed and/or submtted-handlers on your DomainContext. This behaviour has been changed, together with the way loading is done. Now, we’d typically use a LoadOperation<type> for this. Eg, instead doing something like this:
1: // init domaincontext, load customers
2: context = new DomainService1();
3: myDataGrid.ItemsSource = context.Customers;
4: context.LoadCustomer();
which would automatically execute the GetCustomer method (and eventually bind a handler to the submitted-event), you’d now do something like this:
1: CustomerDomainContext CDC = new CustomerDomainContext();
2:
3: public MainPage()
4: {
5: InitializeComponent();
6:
7: LoadOperation<Customer> loCustomer = CDC.Load(CDC.GetCustomerQuery());
8:
9: EventHandler handler = null;
10: handler = (send, args) =>
11: {
12: // do stuff after entities have been loaded
13: myDataGrid.ItemsSource = loCustomer.Entities;
14: loCustomer.Completed -= handler;
15: };
16: loCustomer.Completed += handler;
17: }
(first thing you’ll notice: I’ve used names that make sense now ;-))
What happens here is that the LoadOperation will now take care of executing a query (the one you pass in, an EntityQuery – these are automatically generated from your server side query methods) and thus filling its EntityCollection. If you need to subscribe to said events, you can find ’em on the LoadOperation. So: the LoadCustomer-method is no longer generated on your client. Instead, a GetCustomerQuery-method is generated, which you now pass to a LoadOperation to make sure it gets executed. This is actually quite a nice model to work with – for one, you can now check for each LoadOperations’s completed event, instead of just checking the completed event on your full context!
Next to that, the demo solution also had a means of submitting changesets. This, too, has been changed around. Previously, submitting changes would call context.SubmitChanges(). Now, submitting changes on your context can return a SubmitOperation. In this SubmitOperation, you can check if any errors have occurred, and you can execute some code you want to be executed after submit:
1: private void btnSubmitChangeSet_Click(object sender, RoutedEventArgs e)
2: {
3: if (!CDC.IsSubmitting)
4: {
5: if (CDC.HasChanges)
6: {
7: CDC.SubmitChanges(OnSubmitCompleted, null);
8: }
9: }
10: }
11:
12: private void OnSubmitCompleted(SubmitOperation so)
13: {
14: EntityChangeSet currentChanges = CDC.Entities.GetChanges();
15: grdChanges.DataContext = currentChanges;
16:
17: if (so.HasError)
18: {
19: MessageBox.Show(so.Error.Message);
20: }
21: }
As you can see, checking for changes is still possible by getting the EntityChangeSet from the context’s GetChanges()-method.
New sourcecode has been uploaded, feel free to check it out.