Windows Phone application developers frequently need to access data to present in their applications. Of the variety of facilities available for data services, Microsoft touts oData as solution, which is based on entity and relationship model that allows access to data in REST style (REST stands for representative transfer).
What is oData?
The documentation for Open Data Protocol defines it as a Web protocol for querying and updating data and builds upon Web technologies to provide information from a variety of applications, services and stores.
Where to Get the Client Library?
To get the client library, download the latest version. When you extract the contents, you will find the following files:
DataSvcUtil.exe - tool to generate data classes that represent the data model of an OData service. This tool has a few command-line options which we will discuss later.
Readme.txt - describes the contents of the client library.
System.Data.Services.Client.dll and System.Data.Services.Design.dll - client libraries we will discuss in another article.
If you open up System.Data.Services.Client.dll inside Visual Studio's Object browser, you will notice it contains the following classes:
Figure 1: Open up System.Data.Services.Client.dll in Visual Studio
The DataServiceContext class helps to abstract the implementation against specific data services and also helps maintain change in state of entities on the client, which helps to support change tracking and identity management.
To start with, when using an OData data service provider, we need to generate data classes for consumption in our application.
We use the datasvcutil.exe for this purpose.
For example, if you were developing a NetFlix application, you can use the datasvcutil to generate the data classes.
datasvcutil /uri:http://odata.netflix.com/v1/Catalog/ /out:.\Netflix.cs /DataServiceCollection
Let us look all the arguments supported by datasvcutil.exe.
/uri: The URI hosting the OData Service.
/out : The name of the data class file you want to generate. This file will represent the data objects.
/DataServiceCollection: This ensures that DataServiceCollection classes are generated for each collection in the model. This facilitates easy binding to UI elements.
/in: The path of the file to read the conceptual model from.
/language: You have an option to generate data classes in either VB or C#. By default, the language is C#.
/help: displays the help text.
DataServiceCollection is a dynamic data collection that provides notifications when collection gets modified.
Using Data Services
All operations against data services in the Windows Phone platform are asynchronous. Also, these operations are performed using Begin/End method pairs. The Begin method lets you register a delegate, which can be called by the operating system. This delegate will then call the End method when invoked.
How oData Queries Work
When an application makes a URI based query against an oData service, the BeginExecute method is called. The client library then creates an HHTP GET request message, which is then fired. When an EndExecute method is called, the client library parses the response message to convert it to data service class instances.
Since OData limits data that a query can return, you can use LoadNextPartialSetAsync to load the next set of data.
This article presented an overview of the OData Protocol. In a subsequent article we will learn how to develop a Windows Phone application, which consumes an oData service.