Tuesday, February 23, 2010

Silverlight Data access

Pro Silverlight 4 in C#Pro Business Applications with Silverlight 4Pro Silverlight 3 in C# (Expert's Voice in Silverlight)Silverlight 4 Lab: Intensive Skills Training

One of the bigger beginner misconceptions about accessing data in Silverlight is people looking for some ADO.NET class library in Silverlight. Stop looking, it isn’t there. Remember, Silverlight is a client technology that is deployed over the Internet. You wouldn’t want a browser plug-in to have direct access to your database…as you’d have to expose your database directly to the web. We all know that is generally a big no-no.


So the next logical step is to expose data via service layers. This is how Silverlight can communicate with data. Here are the primary means:

•Web services: SOAP, ASP.NET web services (ASMX), WCF services, POX, REST endpoints

•Sockets: network socket communication

•File: accessing static content via web requests.

Sockets
Sockets are probably the most advanced data access endpoints. These require a socket host to exist and, at the time of this writing, also require communication over a specific port range. If this is acceptable for you, this can be a very efficient and powerful means of communication for your application. I don’t think, however, that this is going to be the norm if your application is public facing on the web – I see sockets right now being more for business applications. Some resources for Sockets:

SDK documentation

Working with data in sockets (Dan Wahlin, MVP) – this is Silverlight 2 but still relevant to understanding the concepts

Working with Sockets requires you to really understand your deployment scenario first before you jump right in and think this will be the best method.



File Access
Silverlight can communicate with local data or data on the web. For local data access, the application does not have direct access to the file system, but rather can read/write data via user-initiated actions using the OpenFileDialog and SaveFileDialog APIs to request and save streams of data to the local user’s machine.
Additionally, you can use plain text files or XML files on the web and have your Silverlight application use standard HTTP commands to read/write that information. Here’s some helper information on using some of these methods:

OpenFileDialog and File Upload

SaveFileDialog

You may find yourselves using these techniques to save settings-based data or use very simple data access.



Web Services
This is the heart of accessing data in Silverlight – through a service layer. Silverlight supports accessing standard ASP.NET web services (ASMX) or WCF-based services using the familiar Add Service Reference methods in Visual Studio that will generate strongly-typed proxy code for you.
Additionally you can use the standard HTTP verbs to access more POX (Plain old XML) or REST-based endpoints. Understanding how to consume these various service types is probably the best time spent a developer can educate themselves on understanding what will be right for their scenario. Here’s some resources:

Consume WCF and ASP.NET Services

Calling services over HTTPS

•.NET RIA Services

Choosing a data access layer (Shawn Wildermuth, MVP)

The third point there, .NET RIA Services, is a new framework that aims to make accessing data simpler and more familiar. The link to the video will walk you through an introduction of that topic. RIA Services is best when you own the database and are hosting services in the same web application that will be serving up the Silverlight application.

No comments:

Post a Comment