c# - Silverlight 4: REST API call data retrieval layer? -


i'm working in .net 4 , sl 4. i'm wanting abstract out data retrieval portion of dal silverlight page's code behind. ideally own dll interface layer between silverlight application , rest api calls. intent not use ria services have existing dal that's making use of dll makes rest api calls.

the issue asynchronous "webclient" call process. know can utilize webclient class make rest call , register asynchronous handler bind results call ui. in case, want abstract out own dll. basically....making synchronous.

by definition asynchronous call has issues here being can't , directly return ienumerable of type.

can point me sample/tutorial being done?

ria makes method call separate dll ienumerable data collection

that method makes rest api call retrieve data, returns ienumerable ria , bound ui.

boling right down fundementals asking able make synchronous call api performs asynchronous task returns when asynchronous task complete. state way want re-combine begin , end phases of asynchronous operation single atomic synchronous operation.

the way acheive block thread making call until end phase of asynchronous operation reached. number of reasons not idea.

if serious using silverlight have swallow asynchronous nature , work in framework rather attempting force synchronous system.

converting synchronous asynchronous

have read of blog converting synchronous code asynchronous code. having read lets imagine code uses synchronous method in new dll called downloadyourdataobjects has imaginary signature:-

 public ienumerable<yourdataobject> downloadyourdataobjects(uri source); 

internally use webclient download string rest base service , converts set of yourdataobject instances. imaginary synchronous code display set of data might be:-

 private void btnloadmydata_click(object sender, routedeventargs e)  {       try       {           loadmydata();       }       catch (exception err)       {           // oops bad happened show err.       }  }   private void loadmydata()  {       dataitemslistbox.itemssource = downloadyourdataobjects(someuri);  } 

since silverlight webclient asynchronous need convert whole chain of code work in asynchronous manner.

using asyncoperationservice blog first need convert downloadyourdataobjects return asyncoperation instead. have signature (see later implementation idea):-

public asyncoperation downloadyourdataobjects(uri source, action<ienumerable<yourdataobject>> returnresult); 

the usage code this:-

 private void btnloadmydata_click(object sender, routedeventargs e)  {       loadmydata().run(err =>       {           if (err != null)           {               // oops bad happened show err.           }       });  }   private ienumerable<asyncoperation> loadmydata()  {       yield return downloadyourdataobjects(someuri, result =>       {           dataitemslistbox.itemssource = result;       });  } 

this may litte ott in fact isn't more code original "synchronous" version. in simple case loadmydata had 1 operation perform. more complex version of loadmydata may have multiple other operations need asynchronous well. in such case operations other yield points in code, basic logical structure of loadmydata not change original synchronous version.

here example of implementation of downloadyourdataobjects dll supply.

 public asyncoperation downloadyourdataobjects(uri source, action<ienumerable<yourdataobject>> returnresult)  {      return (completed) =>      {          webclient client = new webclient();          client.downloadstringcompleted += (s, args) =>          {              try              {                  returnresult(convertstringtoyourdataobjects(args.result));                  completed(null);              }              catch (exception err)              {                  completed(err);              }          };          client.downloadstringasync(source);      };  } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -