Search code examples
apache-flexsilverlightlazy-loading

Silverlight,asynchronous,lazy loading what's the best way?


I started to use silverlight/flex and immediately bumped into the asynchronous service calling. I'm used to solving the data access problems in an OO-way with one server fetch mechanism or another.

I have the following trivial code example:

public double ComputeOrderTotal(Order order) 
{ 
   double total = 0;
   // OrderLines are lazy loaded
   foreach (Orderline line in order.Orderlines) 
   { 
       // Article,customer are lazy loaded 
       total = total + line.Article.Price - order.Customer.discount;
   }
   return total;
}

If I understand correctly, this code is impossible in Flex/Silverlight. The lazy loading forces you to work with callbacks. IMO the simple expample above will be a BIG mess.

Can anyone give me a structured way to implement the above ?

Edit:

  • The problem is the same for Flex/Silverlight, pseudo code would do fine
  • Its not really ORM related but most orms use lazy loading so i'll remove that tag
  • The problem is lazy loading in the model
  • The above example would be very doable of all data was in memory but we assume some has to be fetched from the server
  • Closures don't help since sometimes data is already loaded and no asynchronous fetch is needed

Solution

  • C# 5 async / await construct will almost exactly what I want..

    watch presentation by anders hejlsberg