Chapter 12

An Azure Datamarket Overview

What’s In This Chapter

  • Understanding Windows Azure Datamarket
  • Creating and managing your Marketplace account
  • Browsing and subscribing to published content
  • Understanding and using Datamarket datasets

The amount of data available for applications is incredible—and unfortunately, it’s locked behind corporate walls. If you can navigate the multitude of access methods to get to the data, that data is invariably presented in various formats. Accessing different data sources from different vendors—or even the same vendor—and then corralling and controlling that data can be a nightmare. But you can simplify data publishing and even consumption using a cloud-based solution that provides access to disparate data from multiple sources via a common format and access. Enter Azure Datamarket.

What Is Windows Azure Datamarket?

Windows Azure Datamarket is a subset of the Windows Azure Marketplace wherein a single Marketplace account can purchase applications and data. Datamarket was the first area in the Azure Marketplace. Microsoft has since introduced Applications published to Datamarket, which is not limited to the .NET and Windows world. Currently located at https://datamarket.azure.com/, the Marketplace gives users access to a store of applications and datasets for purchase. Figure 12-1 shows the Marketplace homepage.

Introduced as code-name Dallas, and based on common nonplatform-specific web technologies such as HTTP and AtomPub, Datamarket provides a single location where content owners can publish and sell content to the masses using a standardized publish/subscribe model, and which users can access from a wide variety of platforms and applications.

The benefits of Marketplace are as follows:

  • Organizations with vast amounts of content can use Marketplace to advertise and monetize their data to a large audience. Using Marketplace, publishers do not need to invest in the logistics in creating a storefront, billing and account management. Marketplace takes care of the common functionality required to publish data, leaving publishers free from mundane storefront tasks so that they can concentrate on which data is exposed and how it is queried.
  • Consumers use Datamarket to find, subscribe, and browse using the published data within the terms of agreements. Datamarket provides one-stop shopping for various datasets. It also gives consumers common tools to view and explore data as well as consume data in common (including Microsoft Excel) and custom applications. Marketplace provides the common account management, access controls, and billing to the datasets. From a consumer standpoint, using data from Marketplace ensures a consistent experience and makes it easier for users to work with more data as applications grow.

In summary, Marketplace provides not only data but also the common account, subscription, and billing management for applications. Datamarket provides the foundation for publishing and consuming datasets via OData, basic authentication, and OAuth. This is data in the cloud, which means it’s easy to publish, subscribe to, and work with premium content. This chapter focuses on how to locate, subscribe to, and consume data from Azure Datamarket.

Working with Datamarket

As mentioned before, you can consume data from Datamarket in many different ways—from the Service Explorer, which you learn about later in this section, to Microsoft Excel to custom applications. Datamarket allows anyone to browse a list of published datasets. Before you can actually subscribe to and use a dataset , you must register with Marketplace and create a Marketplace account. The following sections walk you through how to do this.

note.eps
Browsing the available datasets in Datamarket does not require a Markeplace account; however, subscribing to Datamarket and using Datamarket data does.

Creating an Azure Marketplace Account

To start using Azure Data Market you must log in using a Windows Live account. If you don’t have a Windows Live account, Marketplace has an option to create a Windows Live account during the sign-in process. If you don’t have a Marketplace account associated with your Windows Live account, you are prompted for registration information. To sign into Marketplace, follow these steps:

1. Click the Sign In button located in the top-right corner of the Azure Marketplace homepage located at https://datamarket.azure.com/. Figure 12-2 shows the Marketplace homepage with the Sign In button. You are directed to the Registration page, as shown in Figure 12-3.
2. Provide registration information such as your first and last name as well as your e-mail address. Select the check box to agree with the Marketplace offers, and click the Continue button.
3. You must accept the Terms of Use agreement before you can register. Select the check box stating you do indeed agree to the terms of use, and click the Register button to complete your Marketplace registration. You can determine that you have successfully registered and signed in when you see your name located in the page header, as shown in Figure 12-4.

After you complete the registration process and have a Marketplace account, you can sign in and out as needed. You can use the My Account pages discussed in the Managing Your Account section to manage your account.

Browsing Available Datamarket Datasets

Marketplace enables you browse a listing of available published datasets by clicking the Data tab located at the top of the page. The Datamarket homepage enables you to browse, filter, or search for datasets that you may be interested in subscribing to.

Datamarket categorizes datasets by cost, category, and publisher:

  • Cost: The three types of cost are Free, Paid, and Free Trial. Figure 12-5 shows a listing of Free datasets. Free trials are generally limited access for a specific time before transaction costs start to accrue. Specific details for each subscription is available before subscribing.
  • Categories: These are generalized buckets of datasets. A single dataset may be included in multiple categories. Using these three filter categories, you can drill into the datasets to find exactly what you are looking for. The Marketplace also enables you to search for datasets, which can help limit the number of datasets you view at a time.
  • Published by: If you know the content publisher’s name, you can filter results by the specific publisher. For example Zillow, Inc. has many published datasources based on homes and mortgages. You can filter the datasets by the publisher Zillow, Inc.

Reviewing Dataset Details

Each published dataset provides an information page that includes details. Clicking on the title of the published content or the associated icon before the title in the search will refer you to the information page.

Figure 12-6 shows an example of an information page and shows information for the Stock Sonar-Sentiment Service of US Stocks dataset.

warning.eps
Content publishers set how you can access the data, how much data access costs, and any licensing restrictions associated with the published data. Pay close attention to these details because they are different for each set of published content.

The information page includes the following:

  • Title: This is the name of the dataset.
  • Description: This is the content publisher’s description of the dataset.
  • Dataset details: This is found in the lower half of the information page. Some of the details are required and some are optional, with the content publisher determining what optional information is included.
    • Sample Images: Many datasets provide examples of the data used in a custom application.
    • Details: This tab provides key information about supported queries, query parameters (required and optional) as well as the fields returned from the query (refer to Figure 12-7). This is where you must look to determine if the dataset provides the data you require.
  • Publisher Offer Terms: This includes the terms and agreements for using the content. You must agree to those terms in order to subscribe to and use the dataset.
note.eps
Pay close attention to the offer terms. The terms differ from dataset to dataset, and the terms might not support the access required for your application or use.
  • Cost: This can be free or paid and is set by the content publisher. Some published datasets provide limited data access for free or as a trial. Many datasets have a tiered schedule. Refer to Figure 12-6 to see the transaction fee for the Stock Sonar-Sentiment Service of the US Stocks dataset listed in the right column. This dataset uses a tiered cost schedule, meaning that the first 500 transactions per month are free. If you need more than that, you must purchase an option that provides the correct amount of transactions per month.
note.eps
A transaction is currently defined as one page of results with up to 100 results per page. Requesting multiple pages of results from a single query results in multiple transactions. Usage information can be viewed using the My Account pages, which are detailed in the Managing Your Account section.

Subscribing to a Dataset

A subscription to the dataset is required before you can access or view the dataset. This includes both paid and free datasets. To subscribe to the dataset, follow these steps:

1. Click the Sign Up or Buy button located on the dataset’s information page (see Figure 12-6). These buttons (see Figure 12-8) are located with the pricing schedule for a dataset. Datamarket confirms your subscription request by displaying a form containing information associated with the dataset including a link to the terms and cost of the subscription. Figure 12-9 displays the subscription page for www.zillow.com’s MortgageInformationAPIs dataset, a free dataset that provides average mortgage rates and monthly mortgage payments.
2. You must agree to the publisher’s terms and privacy agreement before the subscription request is finalized. Read the publisher’s terms and agreements, and select the associated check box.
3. Click the Sign Up or Buy button to finish the request. You must repeat the subscription process for each data set you plan to access. When subscribing to a paid dataset, the confirmation page includes payment option information.

Working with the Service Explorer

Datamarket provides the Service Explorer interface to access subscribed data without creating a custom application. End users can use the Service Explorer tool to query and view data in many different formats including tabular, chart, or raw XML. This interface also enables export from Datamarket to various formats and import into existing applications.

You can access the Service Explorer by clicking the Explore This Dataset link (see Figure 12-10) located on the dataset’s information page. This link is only available when you are signed into Marketplace and viewing the information page of a dataset you have subscribed to. Clicking the Explore This Dataset link opens the Service Explorer page.

The Service Explorer provides a quick view into the data. As an example, Figure 12-11 shows the Service Explorer querying the MortgageInformationAPIs for the average mortgage rate for Illinois. This dataset queries and finds average mortgage rates and monthly mortgage payments. Users can select the query and any query parameters, and view the results in either tabular, chart, or XML format. Users can export results to file as XML or CSV, or format and open the results in Excel PowerPivot or Tableau. The Service Explorer is a useful tool that enables end users to quickly search and export data from subscribed datasets.

warning.eps
Using Service Explorer incurs any agreed upon transaction fee for the subscription.

End users are not the only audience for Service Explorer. Developers commonly use Service Explorer as a quick way to explore and understand the available queries and data schema while creating an application or creating, or modifying a query.

The process behind Service Explorer involves the following steps:

1. Datasets are exposed as OData, which is a URL query format.
2. The Service Explorer provides a graphical interface that creates the URL search query posted to Datamarket.
3. These URL queries are posted to Datamarket to retrieve the results.
4. The posted query and its parameters display in the Service Explorer tool.
5. The results are displayed in the Service Explorer window.

The query displays above the results windows Applications that have a specific fixed query requirement; the Service Explorer can be used to create the URL query which can then be copied into the application code. You can use simple string manipulation such as String.Format to modify the URL parameters. The query created to display the average mortgage rate for Illinois is:

https://api.datamarket.azure.com/data.ashx/Zillow/MortgageInformationAPIs/GetRateSummary?State=%27IL%27&$top=100.

The Service Explorer can also display the result set as XML. This is particularly useful for non-.NET developers who may not have a proxy to simplify development. Viewing the results of the query as XML allows the developer to see the schema of the results and aids in parsing the XML. Listing 12-1 displays the result for the preceding URL query in XML. This particular query is formatted as AtomPub.

Listing 12-1: Results of a Query Viewed as XML

<feed xmlns:base="https://api.datamarket.azure.com/Data.ashx/Zillow/
MortgageInformationAPIs/GetRateSummary" 
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
xmlns="http://www.w3.org/2005/Atom">
<title type="text" />
 <subtitle type="text">
    The GetRateSummary API returns the current rates per loan type from Zillow 
Mortgage Marketplace. Current supported loan types are 30-year fixed, 15-year 
fixed, and 5/1 ARM. Rates are computed from real quotes borrowers receive from 
lenders just seconds before the rate data is returned. The GetRateSummary API 
returns rates for a specific state if the optional state parameter is used
   </subtitle>
 <id>https://api.datamarket.azure.com/Data.ashx/Zillow/MortgageInformationAPIs/
GetRateSummary?State='IL'&amp;$top=100</id>
 <rights type="text"> Zillow, Inc., 2008</rights>
 <updated>2012-03-01T12:53:17Z</updated>
 <link rel="self" href="https://api.datamarket.azure.com/Data.ashx/
 Zillow/MortgageInformationAPIs/
GetRateSummary?State='IL'&amp;$top=100" />
 <entry>
  <id>https://api.datamarket.azure.com/Data.ashx/Zillow/MortgageInformationAPIs/
GetRateSummary?State='IL'&amp;$skip=0&amp;$top=1</id>
  <title type="text">RateSummaryEntity</title>
  <updated>2012-03-01T12:53:17Z</updated>
  <link rel="self" href="https://api.datamarket.azure.com/Data.ashx/Zillow/Mortgage
  InformationAPIs/
GetRateSummary?State='IL'&amp;$skip=0&amp;$top=1" />
  <content type="application/xml">
   <m:properties>
    <d:ThirtyYearFixedToday m:type="Edm.Double">3.8</d:ThirtyYearFixedToday>
    <d:ThirtyYearFixedCountToday m:type="Edm.Double">
      94
    </d:ThirtyYearFixedCountToday>

    <d:FifteenYearFixedToday m:type="Edm.Double">
      3.04</d:FifteenYearFixedToday>
    <d:FifteenYearFixedCountToday m:type="Edm.Double">
       87
    </d:FifteenYearFixedCountToday>
    <d:FiveOneARMToday m:type="Edm.Double">2.84</d:FiveOneARMToday>
    <d:FiveOneARMCountToday m:type="Edm.Double">28</d:FiveOneARMCountToday>
    <d:ThirtyYearFixedLastWeek m:type="Edm.Double">
       3.75
    </d:ThirtyYearFixedLastWeek>
    <d:ThirtyYearFixedCountLastWeek m:type="Edm.Double">
      5695
    </d:ThirtyYearFixedCountLastWeek>
    <d:FifteenYearFixedLastWeek m:type="Edm.Double">
      3.0
     </d:FifteenYearFixedLastWeek>
    <d:FifteenYearFixedCountLastWeek m:type="Edm.Double">
       4950
    </d:FifteenYearFixedCountLastWeek>
    <d:FiveOneARMLastWeek m:type="Edm.Double">2.59</d:FiveOneARMLastWeek>
    <d:FiveOneARMCountLastWeek m:type="Edm.Double">
      2928
    </d:FiveOneARMCountLastWeek>
   </m:properties>
  </content>
 </entry>
</feed>

Managing Your Account

Now that you have an account and have subscribed to one or more datasets, you’ll need to know how to manage your account settings, which includes managing your account keys and datasets. You can manage your account from the My Account pages, which you access from the My Account link located in the top menu bar on the Windows Azure Marketplace interface. This page has links on the left side:

  • Account Information: Clicking this displays the Account Details page, as shown in Figure 12-12. This page contains your contact information, customer ID, and primary account key. This key should be treated like a password and not provided to everyone. From this page you can edit your account information or view your billing history in the Microsoft Billing and Account Management pages. You cannot edit your primary account key or custom ID.
  • My Data: Clicking this displays the My Data page, as shown in Figure 12-13, which includes a list of your subscribed datasets with details such as a transaction count for those subscriptions that are not free. From this page you can view the subscription information, cancel a subscription, open the Service Explorer, or view the accepted publisher terms. The My Data page is also where you monitor how many transactions are left in the billing cycle. Figure 12-13 shows five different datasets of which four have a transaction limit.
  • Account Keys: Clicking this displays the Account Keys page, as shown in Figure 12-14, where you manage your Marketplace accounts. This is one place where you can find all your keys. From this page you may add new account keys, and edit or remove any additional account keys. You cannot remove the default key. These keys are used during the authentication process.

Programming Datamarket

Datamarket data can be exported and used by many existing applications including Excel, PowerPivot, and Tablau (an end user business intelligence tool). Exporting data as XML or as a comma-separated value formatted file (.csv) opens up the number of applications that can use the data. All these tools work well to browse, filter, and explore the data to find some trend or data point.

What about consuming Datamarket data in a mashup or custom application? Datamarket is easily consumed by applications of all types, including .NET or other platforms. Datamarket data can also be consumed by forms-based, web-based, console-based, and mobile applications to meet the requirements of your business.

Datamarket Concepts

Datamarket helps to standardize how you can access various data potentially residing in different data stores in the cloud. Datamarket achieves this by using common well-known schemas and protocols such as OAuth, OData, AtomPub, JSON, and HTTPS. Using these common schemas and protocols allows standardized access to data by .NET applications and non-.NET applications alike.

This section covers core concepts that you will need to know to programmatically work with Datamarket datasets.

OData and WCF Data Services

OData stands for Open Data Protocol, which defines a common access method to data using standard web technologies. This access method adds, edits, deletes, and queries data abstracted from the storage mechanism. Building on common web-based technologies such as AtomPub, JSON, HTTP, and XML, data stores supporting OData can be accessed from any application that can create and process a simple web request. For more information on OData, visit www.odata.org/.

Datamarket data is served up using WCF Data Services, which is a .NET framework supporting the creation and consumption of OData. For the .NET developer, this means that you have the main plumbing required to generate a query to Datamarket and make the results easily available as class entities. There is no need to hand-code web requests, create OData queries, or parse XML results into entity objects. WCF Data Services provides this functionality. For more information on WCF Data Services, go to http://msdn.microsoft.com/en-us/data/bb931106.

note.eps
WCF Data Services provides much of the common plumbing for an OData request. The proxy objects created on the client side require a .NET language. Developers not working in a .NET language can still use web requests and other OData frameworks to query and retrieve data from Datamarket.

Authentication

Datamarket supports two authentication scenarios at this time: Basic Authentication and OAuth. This chapter focuses only on Basic Authentication but understanding the difference between the two scenarios is important for a developer consuming Datamarket datasets.

Basic Authentication is simply passing a user’s credentials along with the web request. As the name implies, it is one the most basic methods to authenticate a user. In the .NET world it is simple to create a NetworkCredential and attach it to the request. Although simplicity is the key with Basic Authentication, it does have its detractors. Basic Authentication uses a single account to control access. There is no management of users; it is all or nothing because everyone uses the single key for access. All transactions incurred with Basic Authentication are assigned to the owner of the key.

OAuth is a token-based authentication system that is increasingly in use because of the network of available social applications. Datamarket can use OAuth to authenticate users, but compared to Basic Authentication, it’s a bit more challenging to implement. Using OAuth in your application to authenticate to Datamarket requires you to register the application with Marketplace. For more information on OAuth and Datamarket, go to http://msdn.microsoft.com/en-us/library/windowsazure/gg193416.aspx.

Query Types

Accessing data involves creating and submitting queries to Datamarket. Content providers determine the queries that are allowed for the dataset. The Details tab located on the dataset’s information page (refer to Figure 12-7) includes the supported types of queries. Figure 12-15 shows the supported queries for the Stock Sonar - Sentiment Service of US Stocks dataset.

Datasets in Datamarket supports two types of queries: Fixed and Flexible. Each dataset may support both types of queries:

  • Fixed queries: These are the most common query types and are predefined queries that generally have required and optional parameters. Predefined queries create a limit on how you can access the data; the query creates a “box” around the data. Fixed queries are supported in .NET with a provided proxy class available from the dataset’s detail page. You can use this proxy to simplify the code required to access the dataset. The provided proxy for a Fixed query uses WCD Data Services internally to access the dataset. This chapter includes an example using a fixed query.
  • Flexible queries: Are just that—flexible. Currently there are far fewer datasets that provide Flexible queries. Flexible queries remove the artificial box that Fixed query creates and allows more access to the dataset. Flexible queries support the name/value pair convention for parameters via the URL and support most of the available OData query options. For a current list of supported OData query options, see the Supported OData located at http://msdn.microsoft.com/en-us/library/windowsazure/gg312156.aspx. Flexible queries are supported in .NET by adding a .NET Service Reference to the project.

Building a Fixed Query Datamarket Application

This section covers how simple it is to create an application that uses a Fixed query to retrieve data from a Datamarket dataset. This application queries and retrieves data from the Zillow MortgageInformationAPIs available from Datamarket. The MortgageInformationAPIs dataset provides two fixed queries to retrieve data from the dataset. This application shown in Figure 12-16 uses the GetRateSummary query to retrieve average mortgage rates. To create the application, follow these steps:

note.eps
To complete the demo application you must create a Marketplace account and subscribe to the Zillow MortgageInformationAPIs. The first section of this chapter discusses how to create a Marketplace account and subscribe to datasets.
1. Locate and open the starter project for the Fixed Query application. The project is named Wiley.Datamarket.MortgageRates and is available in the code download for Chapter 12 (search www.wrox.com using the book ISBN). The starter project is a WPF application and includes most of the code and markup not directly associated with the accessing Datamarket.
note.eps
The completed project minus the account keys is available with the chapter content.
2. Download the proxy. Fixed queries are supported by a predefined proxy supplied from Datamarket. You must downloaded the proxy (shown in this step) and add it to the project (shown in the next step). The link to download the proxy is located on the dataset’s information page. To access the link you must be logged into the Marketplace with an account that has a subscription to the MortgageInformationAPIs dataset. Figure 12-17 shows the .NET C# Class Library link. Click the link and download the proxy to your computer.
3. Add the proxy class to the project. Right-click the project name in the Solution Explorer pane, click Add, and then click Existing Item. Navigate to the downloaded MortgageInformationApiContainer.cs file, and click Add. Figure 12-18 shows the proxy class added to the project.
note.eps
If you review the MortgageInformationApiContainer, you can see a small amount of code for the specifics of the two queries: GetRateSummary and GetMonthlyPayments. These methods create and return a DataServiceQuery. When executed the DataServiceQuery retrieves the data from the data service in DataMarket.
4. Include an assembly reference. The proxy requires a reference to the System.Data.Services.Client assembly. To include this assembly reference in the project, right-click the project name in Solution Explorer. Then select Add Reference. Select the .NET tab from the Add Reference Dialog box, and select System.Data.Services.Client. Then click OK. Figure 12-19 shows the Solution Explorer with the System.Data.Services.Client reference.
Now you’re set to query and display data retrieved from Zillow’s MortgageInformationAPIs dataset.
5. Add the using statement. You do this by right-clicking MainWindow.xaml in the Solution Explorer and clicking View Code to open the MainWindow.xaml.cs file. Add the following Using statement to the list of existing Using statements:
Using Zillow;
6. Create a results collection. You need to create an ObservableCollection-based property to contain the results. The user interface uses data binding to display RateSummeryEntity objects returned from Datamarket. Add the code in Listing 12-2 to the MainWindows.xaml.cs file below the comment //TODO: Add Rates collection (see the bold code).

Listing 12-2: The Completed MainWindow Constructor

private ObservableCollection<RateSummaryEntity> _rates;
public ObservableCollection<RateSummaryEntity> Rates
{
   get { return _rates; }
}

public MainWindow()
{
  InitializeComponent();
      
  //TODO: Initial _rates collection
  _rates = new ObservableCollection<RateSummaryEntity>();
  this.DataContext = this;
  LoadStates();
  cboState.SelectedIndex = 0;
}
With the results collection defined and initialized, the next step is to create a method to query the dataset and populate the results collection with the result.
7. Query the dataset by creating a method named GetStateMortgageAvg. This method accepts a string parameter of a state abbreviation. Listing 12-3 shows the completed method.

Listing 12-3: The GetStateMortgageAvg Method

private void GetStateMortgageAvg(string stateAbbreviation)
{
  Rates.Clear();
  MortgageInformationApiContainer mortgageClient = 
         new MortgageInformationApiContainer(
                                              new Uri(
               "https://api.datamarket.azure.com/Zillow/MortgageInformationAPIs"));

  NetworkCredential credentials = new NetworkCredential();
  credentials.UserName = "[Your Email Account]";
  credentials.Password = "[Your Marketplace Key]";

  mortgageClient.Credentials = credentials;
  //Catch Average selection
  string stateAbr = stateAbbreviation == "AV" ? "" : stateAbbreviation;

  var results = mortgageClient.GetRateSummary(stateAbr);

  foreach (RateSummaryEntity rates in results)
  {
   Rates.Add(rates);
  }
}
warning.eps
To run this application, include your Marketplace e-mail address and the Marketplace key. You can find the key on the My Accounts pages in Azure Marketplace.
8. Complete the ComboBox event handler. You do this by adding a final line of code to the application, which is the call to GetStateMortgageAvg method from the existing cboState_SelectionChanged event handler. This event handler is called when a user selects a state. Listing 12-4 contains the completed cboState_SelectionChanged event handler.

Listing 12-4: The Completed cboState_SelectionChanged Event Handler

private void cboState_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 ComboBox s = sender as ComboBox;
 State selectedState = s.SelectedItem as State;
     
 //TODO: Call GetStateMortgageAvg Function
 GetStateMortgageAvg(selectedState.Abbreviation);
}

The Fixed Query application is now complete. You can build and run the application by pressing F5 in Visual Studio. The application initially calls into GetStateMortgageAvg with the AV abbreviation. This queries the dataset for mortgage rates based on an average of all states. It is in this method that the proxy is used to connect to Datamarket and query the dataset. Notice that a network credential is associated with the request in the GetStateMortgageAgg method. See Figure 12-20.

When you select a state from the drop-down control, the application calls GetStateMortgageAvg passing in the associated state abbreviation. Figure 12-21 displays the current averages for the State of Illinois.

Building a Flexible Query Datamarket Application

This section shows how simple it is to create an application that uses a Flexible query to retrieve data from a Datamarket dataset. This application queries and retrieves data from the WeatherBug Historical Observations available from Datamarket. This dataset provides a limited number of monthly transactions for free and supports Flexible queries. The Historical Observations supports Flexible queries to query WeatherBug stations and historical weather observations. The Flexible query application, as shown in Figure 12-22, uses a Flexible query to return a list of weather stations located within a specific ZIP code. Selecting a specific weather station generates another query to retrieve a set of historical weather observations generated from the selected station.

note.eps
To complete the demonstration application, you need to create a Marketplace account and subscribe to the WeatherBug Historical Observations. Refer to the first section of this chapter to create a Marketplace account and subscribe to datasets.
1. Open the starter project. Locate and open the starter project for the Fixed Query application. The project is named Wiley.DataMarket.WeatherBug and is available in the code download for Chapter 12 (search www.wrox.com using the book ISBN). The starter project is a WPF application and includes most of the code and markup not directly associated with the accessing Datamarket.
note.eps
The completed project minus the account keys is available with the chapter content.
2. Add the WeatherBug Historical Observation service reference to the project. Flexible queries are supported by a .NET service reference. Adding the service reference creates the proxies to access the dataset using WCF Data Services from the client. To add the service reference to the project, right-click the project name in the Solution Explorer, and select Add Service Reference. Enter the URL to the service reference in the Address list box. You can find the URL address to the service reference on the information page for the dataset or in the Service Explorer. Enter https://api.datamarket.azure.com/WeatherBug/HistoricalObservations/ into the Address list box, and click Go. After you locate the service, enter a Namespace value of WeatherBugSvc. Figure 12-23 shows the Add Service Reference dialog. Click OK to add the service reference to the project.
3. Set credential values. You do so by right-clicking MainWindow.xaml in the Solution Explorer and clicking View Code to open MainWindow.xaml.cs. Locate the _userId and _pwd variables under the class declaration. Enter the Live ID e-mail and account key associated with your Marketplace account.
4. Create the StationList ObservableCollection property. The StationList ObservableCollection property contains the list of weather stations retrieved from the dataset. The Statlist type is defined and added to the project by the service reference. The user interface databinds to this collection to populate the Station ComboBox. Listing 12-5 includes the complete StationList property. Add this property below the class level variables in the MainWindow class.

Listing 12-5: StationList Property Code

public ObservableCollection<Statlist> StationList
{
  get;
  private set;
}
5. Create the StationObservations ObservableCollection property. The StationObservations ObservableCollection property contains the list of weather observations retrieved from the dataset. The HistHilo type is defined and added to the project by the service reference. The user interface data binds to this collection to populate the grid displaying the station’s observations. Listing 12-6 includes the complete StationObservations property. Add this property below the StationList ObservableCollection property created in step 4.

Listing 12-6: StationObservations Property Code

public ObservableCollection<HistHilo> StationObservations
{
  get;
  private set;
}
6. Initialize the ObserableCollections. The two properties are backed by an ObserableCollection. The properties must be initialized before use. Locate the MainWindow constructor and initialize the two properties by adding the code in Listing 12-7 after the DataContext is set.

Listing 12-7: Initializing Properties

StationList = new ObservableCollection<Statlist>();
StationObservations = new ObservableCollection<HistHilo>();
7. Populate the Station ComboBox. The user interface uses a databound ComboBox to display a list of weather stations retrieved from the dataset. This requires a query against the Datamarket dataset. The query filters the retrieved weather stations located in a specific ZIP code. The skeleton GetWeatherStation method exists in the starter project. Listing 12-8 shows the completed GetWeatherStation method.

Listing 12-8: Retrieving Weather Stations

private void GetWeatherStations()
{
  StationList.Clear();

  WeatherBugSvc.WeatherBugHistoricalObservationsContainer svc = 
   new WeatherBugSvc.WeatherBugHistoricalObservationsContainer(
   new Uri(_svcRootUrl));

  svc.Credentials = new NetworkCredential(_userId, _pwd);

  //Query for stations that have the value of _zipCode.
  //Order by station name
  IEnumerable<Statlist> stations = svc.Statlist
      .Where((station) => station.Zip_Code == _zipCode.ToString())
      .OrderBy((station) => station.Station_Name);

  //execute the query and add each result station to the
  //StationList collection
  stations.ToList().ForEach((station) => StationList.Add(station));
} 
note.eps
Simply creating the query does not immediately execute the query. This query is not executed until the results are requested. In the GetWeatherStations method the results are requested when the stations variable is converted to a list using stations.ToList(). At this time the query is sent to Datamarket and processed. The results are added to the StationList ObservableCollection, which is bound to the user interface.

Listing 12-9 shows the complete MainWindow constructor with the GetWeatherStations method call.

Listing 12-9: Completed MainWindows Constructor

public MainWindow()
{
  InitializeComponent();
  this.DataContext = this;
      
  StationList = new ObservableCollection<Statlist>();
  StationObservations = new ObservableCollection<HistHilo>();
  GetWeatherStations();
}
At this time the application should compile and populate the ComboBox with weather stations.
8. Populate the Observation Grid. After you select a station from the ComboBox, the application can retrieve a collection of observations made from the selected station. This involves another query to the dataset and another transaction. The GetStationHiLos method exists in the starter project and is responsible for retrieving the station observations.

Listing 12-10 includes the complete GetStationHiLos method. Use Listing 12-10 to complete the GetStationHiLos method.

Listing 12-10: The Completed GetStationHiLos Method

private void GetStationHiLos(string stationId)
{
 StationObservations.Clear();

 WeatherBugSvc.WeatherBugHistoricalObservationsContainer svc = new  
               WeatherBugSvc.WeatherBugHistoricalObservationsContainer(
                                                    new Uri(_svcRootUrl));
 svc.Credentials = new NetworkCredential(_userId, _pwd);

 IEnumerable<HistHilo> observations = svc.HistHilo
                  .Where((hilo) => hilo.Station_ID == stationId)
                  .OrderByDescending((hilo) => hilo.Observation_Date);
     
 observations.ToList().ForEach((hilo) => StationObservations.Add(hilo));
}

The GetStationHiLos method is similar to the GetWeatherStation method. This method filters the HistHilo collection for items that have a matching Station_ID value and places those items in the StationObservations property, which is databound to the user interface. The query returns a collection of HistHilo entity objects. The HistHilo entity class was created when the service reference was added to the project.

The GetStationHiLos method should be called when the selected station changes. This requires a transaction for each change of station. To keep the user-interface grid in sync with the currently selected station, the ComboBox’s SelectionChanged event is used. The completed cboStation_SelectionChanged method is shown in Listing 12-11. Two method calls were added. The first SetStationDetails is called to populate the selected station name and location in the user interface. Then the GetStationHiLos is called to retrieve the stations’ observations.

Listing 12-11: The Completed SelectionChanged Event

private void cboStation_SelectionChanged(object sender, 
                                         SelectionChangedEventArgs e)
{
  ClearStationDetails();
  ComboBox c = sender as ComboBox;
  Statlist stationItem = c.SelectedItem as Statlist;
  SetStationDetails(stationItem);
  GetStationHiLos(stationItem.Station_ID);
}

The Flexible query application is now complete. Build and run the application by pressing F5 in Visual Studio. The application initially retrieves the stations for the ZIP code. Selecting a specific station from the ComboBox generates a query to Datamarket for the observable collections for the station. The complete code for example shows the simplicity of accessing data from Datamarket using a Flexible query as most of the common plumbing has been created by the addition of the service reference.

Summary

The amount of data available to the user and developer is massive. The various access methods, authentication requirements, account management, and billing processes make it difficult for an organization to efficiently consume the available data. Simply finding the right data source can be problematic.

Datamarket simplifies the overhead of working with many different data sources. It does so by creating a consistent experience when you work with cloud data regardless of who owns the content or how the content is stored. Using open web technologies allows a reach beyond the Microsoft .NET tools. For those using .NET languages, integrating the data into an application is even easier with the support provided by service references and proxies.

Organizations that have premium content should look to Datamarket in order to reduce the amount of overhead of maintain the account management billing as well as a storefront. Datamarket enables content publishers to focus on content and not plumbing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset