Resumé

WCF DataServices in Windows Phone 7

Cristian Merighi () 0.00

Sample code about how to manage a DataContext the asynchronous way.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

How to manage a WCF DataService in Windows Phone 7 (I may write Silverlight)?
Quite simple task, neither a particularly original subject. I just write this article to fix on "paper" the key code needed to accomplish it so that I can retrieve it with relative ease in the future.
(Syntax in the WPF world is almost identical).

Reading data (C#):
  1. // DataContext proxy instance:
  2. var proxy = new ToolSpare.ToolSpareContext(new Uri("http://myserver/mydataservice.svc/"));
  3. // Build up a query to be executed (cast it as a System.Data.Services.Client.DataServiceQuery) then...
  4. var query = proxy.MovementSet.Where(m => !m.IsIncome).OrderByDescending(m => m.Amount).Take(20) as DataServiceQuery<ToolSpare.Movement>;
  5. // ...execute it asynchronously
  6. query.BeginExecute((asyncResult) =>
  7. {
  8.     // use state token to get the results:
  9.     var state = asyncResult.AsyncState as
  10.         DataServiceQuery<ToolSpare.Movement>;
  11.     var list = state.EndExecute(asyncResult).ToList();
  12.     list.ForEach(outcome => Dispatcher.BeginInvoke(() =>
  13.     {
  14.         // populate some collection in the ViewModel (execute this code in the UI Thread)
  15.         var item = new ItemViewModel { LineOne = outcome.Amount.ToString("c2"), LineTwo = outcome.Caption, LineThree = string.Format("{0:D} {0:t}", outcome.Date) };
  16.         this.Items.Add(item);
  17.     }));
  18. }, /* state -> */query);

Reading data, alternative way (C#):
  1. // using System.Data.Services.Client.DataServiceCollection
  2. DataServiceCollection<ToolSpare.Movement> outcomes = new DataServiceCollection<ToolSpare.Movement>(proxy);
  3. outcomes.LoadCompleted += (s, e) => // inline delegate, do not try this at home! Always detach handlers :)
  4. {   
  5.     // ensure execution in the UI-Thread;
  6.     foreach (var outcome in outcomes)
  7.     {
  8.         var item = new ItemViewModel { LineOne = outcome.Amount.ToString("c2"), LineTwo = outcome.Caption, LineThree = string.Format("{0:D} {0:t}", outcome.Date) };
  9.         this.Items.Add(item);
  10.     }
  11. };
  12. outcomes.LoadAsync(/* query from above -> */query);

Saving data (C#):
  1. // changes have been made to the model, DataContext.UpdateObject(...)
  2. proxy.BeginSaveChanges((asyncResult)
  3.     => this.Dispatcher.BeginInvoke(()=>
  4. {
  5.     // execute code in the UI Thread...
  6.     var ctx = asyncResult.AsyncState as ToolSpare.ToolSpareContext;
  7.     ctx.EndSaveChanges(asyncResult);
  8.     // do something else...
  9. }, null), /* state -> */proxy);

Hope that could be of any help.

Take care. Bye.

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds