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.

Saturday, February 20, 2010

Resource Guide for Visual Studio 2010 RC (Release Candidate)


Introducing .NET 4.0: with Visual Studio 2010 (Expert's Voice in .Net)Pro C# 2010 and the .NET 4.0 Platform, Fifth Edition

Visual Studio 2010 and .NET Framework 4 RC (Release Candidate) now available to everyone for download. 
This post is a brief guide to resources to help you quickly get started and learn about what’s new.

 

Before Installing


Install

Install Some More

Learning More

SharePoint 2010 - LINQ To SharePoint is there

Professional SharePoint 2010 AdministrationSharePoint 2010 User’s Guide: Learning Microsoft’s Business Collaboration PlatformSharePoint 2010 as a Development PlatformPro SharePoint 2010 Solution Development: Combining .NET, SharePoint, and OfficeBuilding the SharePoint 2010 User Experience


LINQ is a new data access paradigm which allows users to express SQL like syntax against a variety of data sources. LINQ can improve performance by allowing the back end data source to decide the best way to solve the query. SharePoint now fully supports LINQ for querying lists so that you can query information from the platform in a more condensed, easier to understand format.

LINQ to SharePoint
  • No CAML Required
  • Entity classes form Business Layer
  • Strongly typed queries and compile time check
  • Intelligence helps query construction
  • Microsoft.SharePoint.Linq.dll
Using LINQ to SharePoint
  • Create Entity Classes
  • Create DataContext
  • Write Queries
In order to use LINQ to SharePoint, you will need to have entity classes created. Rather than having to create them manually which would take a long time, Microsoft provided a small utility that creates all those objects for you called SPMetal (click here for more information). The SPMetal utility works similarly to the SQLMetal utility (extracts SQL metadata from a database and generates entity classes). SPMetal will generate those entity classes which are used in LINQ to execute SharePoint queries. When those queries are executed, the LINQ statements are in turn translated to CAML.

The following is an example of the use of LINQ to query SharePoint :

// Get DataContext from page context
DataContext data = new DataContext(SPContext.GetContext(this.Context).Web.Url);

// Get the SharePoint list
EntityList Customers = data.GetList("Customers");

// Query for customers from London
var londonCustomers = from customer in Customers
                      where customer.City == "London"
                      select customer;

foreach (var londonCust in londonCustomers)
{
Console.Writeline("id = {0}, City = {1}", londonCust.CustomerId, londonCust.City);
}