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

I started using silverlight / flex and immediately ran into an asynchronous service call. I'm used to solving data access problems in the OO way with a single server fetch mechanism.

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 not possible in Flex / Silverlight. Spoofing makes you work with callbacks. IMO the simple example above would be a BIG mess.

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

Edit:

  • The problem is the same for Flex / Silverlight, the pseudocode is do fine
  • This is not actually an ORM, but most of the ORMs use lazy loading, so I will remove this tag
  • The problem is lazy loading in the model
  • The above example will be very helpful for all data in memory, but we assume some of it needs to be fetched from the server
  • Closueres does not help, because sometimes the data is already loaded and asynchronous fetching is not required
0


source to share


7 replies


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



watch presentation byers hejlsberg

0


source


Yes. I have to agree that O / R mapping is usually done on the server side of your application. In Silverlight, asynchronous execution is the desired pattern to use when working with services. Why services? Because as I said, there is currently no O / R mapping tool that can be used on the client side (SilverLight). The best approach is to have your display O / R data rendered by a service that can be consumed by the SilverLight application. For now, it is best to use Ado.Net DataServices for data transport and client side for data manipulation with LINQ to Services. What's really interesting about ADS (formerly Astoria project) is that it is intended to be used with Entity Framework.but the good people have implemented IQueriable support as well, so basically you can plug in any data provider that supports LINQ. To name a few, you might consider Linq To Sql, TelerikOpenAccess , LLBLGen, etc. To update the updates to the server, the data source is required to support ADS IUpdateable.



You can see exactly how this can be done in the blogposts series I have prepared here: Getting Started with ADO.NET Data Services and Telerik Open Access

+1


source


I can't talk to Silverlight, but Flex is a client-side web browser technology and doesn't have any database driver built into the Flash runtime. Instead, you can perform HTTP protocol interactions with the web server. It is present on the mid-tier web server where you will be doing the ORM regarding the database connection, like Java JDBC. Hibernate ORM and iBATIS are two popular options in the Java middleware space.

Also, because of this:

Distributed computing errors

You are not performing synchronous interactions with the Flex client with middle tier services. Synchronous network operations have become verboten these days and are a sign of poor application development - since, due to the reasons listed in the link above, an application can (and often will) show a very poor user experience.

Instead, you make asynchronous calls to retrieve data, load the data into the model object of your client application, and perform operations on the model. With Flex and BlazeDS, you can also pass middle tier data to the client and update client model objects asynchronously. (Data binding is one way to respond to updated data in an event-driven manner.)

All of this probably seems very far from the nature of the request in your post, but your post indicates that you are completely incompatible with how to understand client-side technologies that have asynchronous and event-driven programming baked into their fundamental architecture. These RIA client technologies are designed specifically for this. Therefore, you will need to learn their way of thinking if you want to have a good and productive experience using them.

I'll cover this in more detail and from a Flex perspective in this article:

Flex Async I / O vs Java and C # Explicit Threading

+1


source


In my direct experience with Flex, I find this discussion getting too complicated.

Your OO conceptual view is no different between sync and async. The only difference is that you are using event handlers to interact with the host in the DAL, not with something returned by a method call. And this often happens solely on the host side and has nothing to do with Flex or Silverlight. (If you're using AIR for a workstation application, then it might be in client code, but the same applies if you're using extended AJAX. Silverlight, of course, doesn't have an AIR equivalent.)

I was able to build whatever I needed without any other changes needed to host async files.

+1


source


Flex has a single threaded model. If you make a synchronous call to the web server, you will block the entire GUI of the application. The user will have the application frozen until the call ends (or exits from a network error state, etc.).

Of course, real RIA programs are not written that way. Their graphical interface remains accessible and responsive to the user with asynchronous calls. It also allows you to have real progress indicators that offer undo buttons and such if the nature of the interaction warrants this.

Old, bad experience with Web 1.0 applications showed synchronous behavior when interacting with the web tier.

As pointed out in my linked article, the asynchronous single-threaded model combined with ActionScript3 closure is a good thing because it is a much simpler programming model than the alternative programming model - creating multi-threaded applications. Multithreading is an approach to writing Java Swing or C # .NET WinForm applications for client-server applications to achieve the same flexible, flexible time user experience in a GUI.

Here's another article that covers this whole subject of asynchronous messaging / event-enabled distributed application architecture:

Building efficient enterprise distributed software systems data-driven communication and behavior communication

+1


source


Silverlight is a client-side technology, and Object-Relational rendering happens entirely on the server. Therefore, you should forget about the ORM in Silverlight.

Following your example, you need to create a web service (SOAP, REST ...) that can give your silverlight client a complete Order object. Once you have an object, you can work with it without communicating with the server in the usual synchronous way.

0


source


Speaking of Silverlight, you should definitely check out RIA services .

It just brings the DataContext from server to client from where you can request it asynchronously (no need to manually write WCF services, it's all done by RIA).

0


source







All Articles