The following is an excerpt from John Papa's new book, Data-Driven Services with Silverlight 2 (adapted for the web).
You've learned how Silverlight can produce stylish interfaces and highly interactive applications in a variety of browsers, and now you want to build a Silverlight application. Not just a bouncing ball, an embedded video, or a spiffy-looking series of buttons, but a walking, talking, fully functional line-of-business application. Of course, you still want the snazzy interface, too. The good news is that you can have it all with Silverlight 2.
In this chapter, I will review some of the critical areas that you need to be familiar with to develop data-driven Silverlight applications, including required software installations and tools. Before developing with Silverlight 2, it is important to understand the major differences between versions 1 and 2 of Silverlight. This chapter will review those differences and point out why each advancement in Silverlight 2 is important to developing data-driven applications.
One important advancement that Silverlight 2 introduces is the ability to write .NET code. This allows developers to leverage their existing skills to create robust Silverlight 2 applications. The .NET 3.5 language features in Silverlight 2, such as LINQ and implicitly typed variables, can also significantly aid in the development of these applications. This chapter will discuss some of these .NET 3.5 language enhancements as they are used throughout this book. I will also walk through the steps of creating a simple data-driven Silverlight 2 application to help get you started.
The Importance of Data Access
Beyond the rich user interfaces, video streaming, and stylish templates in Silverlight 2 lies a robust framework that works very well with various types of data sources. Silverlight applications run in the client machine's browser, where they have the Internet between them and any server applications. Silverlight applications are disconnected from remote data sources, so they must communicate with remote servers through various types of web services to send and receive data.
Silverlight 2 provides several ways to get data from remote servers using HTTP and sockets. Some of the most popular and useful techniques are to communicate with a SOAP-based web service on a remote server (as we'll explore in Chapters Chapter 5 and Chapter 6), and to receive data from a REST service (which we'll discuss in Chapters Chapter 7-Chapter 9). Several tools and techniques are available for receiving the data, manipulating it, binding it, presenting it to the user, and handling all of the data-driven aspects of the application, all inside Silverlight 2.
Most applications require some sort of data interaction. The data might be from a database, an RSS feed, a web service, or a REST service that returns Plain Old XML (POX). Silverlight applications can communicate with services and return data in a variety of ways. Although Silverlight can be a very rich and compelling tool for end users, the data for the application is just as important as its delivery.
Data access has taken on several meanings in recent years. Data access really is a term that describes the act of accessing data from any number of sources. This can mean accessing data from a database directly through ADO.NET, the ADO.NET Entity Framework, NHibernate, LLBLGen Pro, Enterprise Library, or some custom business object and data access layer. It can also mean accessing data through web services, ASMX, Windows Communication Foundation (WCF), RSS feeds, REST-style web services, and HTTP requests. Obviously, you can access data in several ways, and once you've retrieved it, you often need to manipulate it in some way. Developers often use LINQ to organize data that has been retrieved so that it can be in a more usable shape for the task at hand. All of these aspects define data access, and you can use all of these directly or indirectly through Silverlight 2.
Silverlight is a fantastic tool for designing rich interfaces that work in various browsers. However, its extensive support for data programmability is what makes Silverlight a viable solution for business applications. This book arms the reader with the knowledge to build visually and functionally robust applications with Silverlight 2.
Before diving into any examples in this book, make sure that you have the prerequisites for Silverlight 2 installed and set up. You must install the Silverlight 2 runtime to view and run Silverlight applications in the browser. This runtime works in (and is officially supported in) Internet Explorer, Google Chrome, Firefox, and Safari. Runtimes are available for Windows and Mac OS X. The following browsers support Silverlight 2:
- Microsoft Internet Explorer versions 6.0, 7.0, and 8.0 beta
- Mozilla Firefox versions 1.5, 2.0, and 3.0
- Apple Safari versions 2.0 and 3.0 beta
- Google Chrome
Silverlight 2 Tools is an add-on to Visual Studio 2008 that allows you to create Silverlight 2 applications using .NET and Silverlight 2. The Silverlight Tools installation program also installs the Silverlight 2 runtime and the Silverlight 2 SDK. The Silverlight 2 SDK contains examples, documentation, and tools to assist in the development of Silverlight applications.
The following components are required for developing and experiencing Silverlight 2. All of these are contained within a single installation file:
- Silverlight 2 Runtime
- Silverlight 2 SDK
- Silverlight 2 Tools for Visual Studio
The following debugging tools can greatly assist in identifying communication problems with Silverlight 2 applications and services. All are free and can clarify problem points as well as help you to identify solutions. Appendix B discusses tips and tricks when using these tools to debug communications between Silverlight 2 and services.
- Firebug (for Firefox)
- Web Development Helper (for Internet Explorer)
- Fiddler2 (for all network traffic sniffing)
It is also recommended that you install Visual Studio 2008 and Expression Blend with SP 1. Visual Studio 2008 is great for editing XAML, using IntelliSense, and of course, writing .NET code for Silverlight 2 applications. Expression Blend is fantastic for arranging complex designs and layouts within Silverlight 2. Both are valuable tools that you should use side by side when developing with Silverlight 2.
Silverlight 2 Features
Silverlight 2 introduced support for .NET code in Silverlight applications, but that is not the only major improvement it offered. Table 1-1 lists many of the major improvements to Silverlight 2, including several that are integral to developing data-driven applications with Silverlight. Table 1-1 singles out these specific features, as it is important to be aware of how they can help developers build solid, data-driven applications with Silverlight 2.
Framework Languages and .NET Support
I cannot overstate the fact that developing a Silverlight application is much easier when you can leverage your existing .NET development experience. This starts with being able to write .NET code in the Silverlight client application to handle various aspects of that application, including event handlers, referencing the extensive .NET Framework, and creating complex user controls. The Silverlight 2 runtime contains a small but powerful subset of the .NET Framework library so that developers can take advantage of their .NET skills and transition to Silverlight 2 with minimal effort.
Self-Describing Data Services
Silverlight 2 can communicate with SOAP-based web services (e.g., using WCF or ASMX) to pass defined data structures between a Silverlight client and a remote server. These services expose a contract through WSDL with which the client can communicate by generating proxy classes. This contract defines the services that you can call and how to call them, and it exposes the data structures that you can pass into and out of the services. This is key to exposing web services that return or accept serializable entities as parameters. With contractual data services, Silverlight 2 can communicate with remote services that return entities from LINQ to SQL, NHibernate, the ADO.NET Entity Framework, or even custom domain entity models. It also opens the door to being able to communicate with third-party SOAP-based services that expose WSDL, such as Live Search.
Loose Coupling with Data Services
Silverlight 2 can also consume data services without generating proxy classes using SOAP, RSS, Atom, JSON, POX, or REST. Many sources of information are available that return data using a noncontractual service. For example, Digg.com, Amazon.com, Twitter.com, and Flickr.com all expose REST services that return XML. Silverlight 2 applications can request information from all of these services and integrate the results into an application. In Chapters Chapter 7-Chapter 9, we will delve deeply into these areas, as well as discuss how to communicate with them across domains and how to manage the XML and JSON they may return via LINQ to XML and LINQ to JSON, respectively.
The new control model that Silverlight 2 provides makes it much easier to write Silverlight 2 applications. You can organize the new controls within the XAML layout panels, such as
StackPanel, to create rich interfaces. The controls support XAML-based data binding and dependency properties, which we will discuss in detail in Chapters Chapter 2-Chapter 4.
Silverlight 2 introduced more than two dozen controls that you can use to build Silverlight 2 client applications. It offers layout panels such as
Canvas, as well as several input controls that you can customize using templates and styles. You can completely replace the content of some of the controls to achieve a more complex solution. For example, you can replace the header of a
DataGrid control with a series of controls that allow you to sort the
DataGrid. Or you can replace a button's default look with another element such as an
Ellipse and fill it with another
FrameworkElement such as an image. Chapters Chapter 2-Chapter 4 will dive deeply into the controls and explain how data binding works with them, as well as offer best practices for binding and presenting data. Figure 1-1 shows the controls available in Expression Blend 2 with SP 1 and Figure 1-2 shows the controls available in Visual Studio 2008.
LINQ to Objects and LINQ to XML
Silverlight 2 supports the use of LINQ to Objects, which can simplify the querying of data in arrays and collections in a Silverlight application. LINQ to XML (also supported in Silverlight 2 applications) is very useful when consuming noncontractual data services that return XML. The XML can be read into a variety of XML-consuming libraries, but consuming it with LINQ to XML's objects allows you to query the XML using the familiar and powerful LINQ syntax. Also, with LINQ at your disposal you can combine XML and object data structures in the same LINQ query. Many of the examples in this book will use LINQ in some fashion to query data from XML or objects.
Data-driven applications written with Silverlight 2 make extensive use of LINQ and the .NET 3.5 language enhancements in one way or another, as many of the examples in this book will demonstrate. Because of the importance of these features, it is worth taking a look at how they work. C# 3 and Visual Basic (VB) 9 introduced several language enhancements, some of which are integral to writing queries with LINQ. Silverlight 2 applications can consume XML, JSON, objects, entities from LINQ to SQL, and entities from the ADO.NET Entity Framework, to name a few sources.
Silverlight 2 applications often need to gather values from arrays and/or lists of custom entities (e.g., a
List). In these cases, LINQ to Objects comes in handy, as it allows you to query any
IEnumerable list. Although you can consume and manage XML through a variety of .NET libraries, such as
XmlReader, LINQ to XML provides a simpler and often more powerful way to consume XML. This makes it easy for Silverlight applications to consume XML from RSS feeds or REST services and massage the data without resorting to verbose loops. LINQ queries can also join a list of objects and XML, which is very useful when an application is pulling data from multiple sources. This book makes extensive use of LINQ to Objects and LINQ to XML in several chapters in appropriate places where Silverlight applications can take advantage of them.
LINQ to Entities is used heavily in querying the Entity Data Model that the ADO.NET Entity Framework produces. The ADO.NET Entity Framework builds an Entity Data Model that maps an object-oriented conceptual model of entities to a relational store, such as SQL Server (although the ADO.NET Entity Framework supports a provider model, so other database servers such as Oracle are also supported). Queries are written with LINQ to Entities against the conceptual model and the queries are translated through the mapping layer to SQL statements that execute against the relational store. Once the entities have been returned from the LINQ to Entities query, the entities can be serialized and passed to other application tiers. For example, a Silverlight client application could request an entity from a WCF service on a remote server that exposes entities from the Entity Data Model. The Silverlight application could then use LINQ to Objects on those entities as well.
The fourth flavor of LINQ that this book will discuss is LINQ to SQL. LINQ to SQL allows a one-to-one mapping from entities to SQL Server objects. It does not support the rich mapping schema that the ADO.NET Entity Framework supports; neither does it have a provider model, so it works only with SQL Server. For example, you can write queries with LINQ to SQL that return entities that can then be serialized and passed to a Silverlight application.
Silverlight 2 supports code to be written using .NET that includes many features, such as generics, collections, LINQ to Objects, and LINQ to XML. This support, along with the new language enhancements in Silverlight 2, allows developers to create rich, data-driven applications using Silverlight 2 and their existing .NET skill sets. The new control suite, XAML binding techniques, LINQ support, and rich communications features in Silverlight 2 greatly simplify the process of developing data-centric, line-of-business applications.