This article describes a new, important addition to Visual Studio LightSwitch 2012, which is the support for data sources of type Open Data Protocol (OData). I'll explain how to consume OData services in your LightSwitch applications. In part 2 of this series, I'll show you how to expose LightSwitch data sources as OData services to other clients.
A Brief Introduction to OData
Exposing data through the Web—or, more generally, through a network—is a very common need for business software. Typically this goal is accomplished by exposing data via services that can be consumed by several clients. In this kind of scenario, the problem is that there are many ways (and thus APIs) to expose and consume data. The Open Data Protocol (OData for short) provides a standardized way to query and update data, based on the representational state transfer (REST) approach. Basically, REST-ful services support HTTP requests as well as verbs such as GET, POST, PUT, and DELETE; they also allow querying data via the so-called "query strings."
OData derives from the WCF Data Services; if you've already had a chance to work with this platform, you'll be totally familiar with OData. In a typical scenario, an OData service is hosted on a web server and exposed through the Web or a local network via an .Svc file (that is, WCF). OData exposes XML feeds that can be accessed in many ways, including via LINQ. Figure 1 shows how to query an OData service via an HTTP request directly from the browser.
Figure 1 Querying an OData service via HTTP requests.
Now let's move on to examining how to use OData services in Visual Studio LightSwitch, and how to consume such services as data sources—without writing a single line of code.
OData and LightSwitch
Visual Studio 2012 introduces native support for OData in LightSwitch. More specifically, LightSwitch 2012 allows you to do the following:
- Consume OData services produced by other people, in order to extend applications with external data
- Expose any data source used in a LightSwitch application as an OData service, making it possible for other clients to take advantage of your data in the form of an XML feed
With particular regard to the first point above, OData is a new type of external data source to which LightSwitch can connect, joining SQL Server, SQL Azure, SharePoint 2010, and WCF RIA Services. Here's the tremendous benefit: LightSwitch is capable of mapping data from OData services to entities. This fact implies that you'll be able to manage entities—creating queries, handling the business logic, and adding relationships to other tables—even if they're in different data sources. As a LightSwitch developer, you don't need to know what's going on under the hood; you just work with entities in the usual way.
LightSwitch supports both reading and writing data against OData. A lot of public OData services are read-only, so remember that fact if you're attempting to save changes back to a read-only service; a LightSwitch application will show an error message. In part 2 of this series, you'll learn how LightSwitch can expose the intrinsic database and other data sources as OData services to the network; for now, you'll learn how to enhance your applications by consuming existing services.
Choosing Existing OData Services
Lots of producers offer data sets as OData feeds. A variety of OData feeds are available for a plethora of information, such as city crime, transportation, government information, weather conditions, and so on. Two websites offer interesting OData services that you can use in your LightSwitch applications:
- Windows Azure Data Market. In this cloud-based portal, you can find data sets based on the OData protocol, as well as fully-functional applications offered through the cloud. The Windows Azure Data Market requires signing up with a Windows Live ID. Once signed in, you can find a lot of public data sets. Not all are free; many are paid services, and some producers offer a variety of prices based on the number of transactions.
- Ecosystem page of the OData Protocol portal. In particular, the Live Services section of the page summarizes available OData services from several producers, as represented in Figure 2.
The Windows Azure Data Market is certainly the more professional place for finding OData services, but it requires a Windows Live ID, and the web portal automatically requires switching to the version based on your language. For an example with the Windows Azure Data Market, check out Beth Massi's blog post "Enhance Your LightSwitch Applications with OData."
Figure 2 A list of OData services available in the OData portal.
By clicking the Browse link at the right side of each service listing, you can see the list of entity sets exposed by the OData service as an XML feed, inside your web browser. For this article, you'll consume the Netflix public OData service, in order to create a LightSwitch application that displays a catalog of movies based on certain criteria. Netflix is a web portal where you can rent movies for online streaming; the rental service is not available in all countries, but you can still consume the data service and show the results in LightSwitch. The URL of the Netflix service can be found in the OData Protocol portal. We'll use it shortly.
Consuming the OData Service
The goal of the sample application is to display a list of movies available on Netflix, including information such as available formats, awards, and so on. Let's get started.
- Begin by creating a new LightSwitch application in Visual Studio 2012. When the project is ready, click Attach to External Data Source. The Attach Data Source Wizard displays a new item called OData Service, as shown in Figure 3. Select this item and then click Next.
- The next step requires entering the OData service address and specifying credentials, as demonstrated in Figure 4. Enter the Netflix OData service address in the OData Source Address box. Notice that you can flag the "Attach to this data source as read-only" checkbox if you want to use the service only for reading information (or if you know in advance that it's a read-only service.) In the Login Information group, select None. (The OData service of Netflix doesn't require authentication.)
- When ready, click the Test Connection button to test the connection against a particular entity before going on to the next step of this procedure. For example, in the Test Connection dialog, select the Titles entity and then click Test. If the connection succeeds, you'll be prompted with a confirmation message, as shown in Figure 5.
- The next step is selecting which entity sets exposed by the OData service will be used in LightSwitch. For this example, select all the available entities, as shown in Figure 6, to demonstrate how LightSwitch can also handle relationships. (Notice that LightSwitch automatically proposes a name for the new data source, based on the data service's name.)
- Click Finish. A warning message appears, informing you that some custom data types in the service are not supported by LightSwitch, and therefore columns of that type will not be imported. In this particular case, the Netflix service also has many-to-many relationships that aren't supported and won't be mapped. Accept the LightSwitch warning by clicking Continue in the warning dialog.
Figure 3 The new OData Service connection option.
Figure 4 Establishing a connection to an OData service.
Figure 5 Testing the connection to the OData service.
Figure 6 Selecting entity sets.
In order to understand how you're going to use the new data source, double-click the Titles table in Solution Explorer. Figure 7 shows how the Table Designer appears at this point.
Figure 7 The Table designer shows how entities have been mapped to the service entity sets.
It's easy to see how LightSwitch has been able to generate a new entity for each kind of information exposed by the service, mapping data types and required properties correctly. At this point, you can choose different data types for a more precise mapping; for example, changing DateTime to Date. Also, notice how LightSwitch correctly recognizes one-to-many relationships, making them visible in the designer. You can manage entities and relationships exactly as you're used to doing with other kinds of data sources.
As an example of manipulation, and since we want to show the list of available movies, let's change the Summary property in the Title entity. By default, the Summary property is the Id property, but it's more appropriate to change it to the name of the movie for this example. Follow these steps:
- Make sure that the Title entity is open in the Table Designer.
- Click Title.
- In the Properties window, expand the Summary property drop-down box and select Name.
- At this point, we can add a new List and Details screen, demonstrating how easy it is to display information from the OData service—including entities with relationships. In Solution Explorer, right-click Screens and choose Add Screen. In the Add New Screen dialog, under Additional Data to Include, select Title AudioFormats, Title Awards, and Title ScreenFormats (see Figure 8). The Title Details checkbox should be selected by default; if not, be sure to select it.
- After the new screen has been added to the project, simply run the application. On the left side of the screen is the list of available movies, based on the specified Summary property. On the right side of the screen are the details for the highlighted movie, including related information such as awards and available formats (see Figure 9).
Figure 8 Creating a new List and Details screen.
Figure 9 The sample application shows data from the OData service.
The full data set returns the complete list of movies from Netflix, which can be too much. Imagine that you want to display only movies released after 2005 that have been rated by viewers. In Solution Explorer, right-click the Titles table and select Add Query. When the Query Designer appears, call the new query FilteredTitles and specify filters to reproduce those in Figure 10.
Figure 10 Designing a query over the OData service.
The ReleaseYear property is of type Integer, so it's possible to make comparisons with math operators. Notice that the query also implements sorting based on the DateModified property. If you add a new List and Details screen based on the new query and run the application again, the result will show only movies released since the beginning of 2006 that have been rated by viewers (see Figure 11).
Figure 11 The application shows the result of a query.
This just confirms the fact that you can manage a data set coming from OData in the same way you manage data from other data sources.
Free Services with Read/Write Support
LightSwitch has complete control over OData feeds, which means that the runtime can certainly save back and update data to the service, if it supports update operations. Since LightSwitch provides a unified model for accessing different data sources, including OData, the runtime automatically implements whatever is necessary to handle this kind of operation for you. In other words, you won't have to write any code (except for custom business logic) to implement saving and updating operations—LightSwitch does it all for you. The OData Protocol portal offers a free live service that you can use for testing and that supports reading and writing data, which is the Read-Write OData Sample Service. Please read the usage information in the OData portal carefully before consuming it in your LightSwitch application, and remember that it's only for testing purposes.
The upcoming OData support in Visual Studio LightSwitch is a huge benefit because it allows you to integrate your own data with feeds produced by other people. This capability gives you the opportunity to create powerful applications that can offer information created, updated, and maintained by others—without having to change the way you manage either the data or the business logic.
Following is a list of useful references that you can check out to learn more about the OData protocol: