.NET Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Leveraging Windows Azure Mobile Services Data for the WOWZAPP Windows 8 App

11.30.2012
| 3725 views |
  • submit to reddit

In one of the previous articles I talked about a way to create a hub experience, using the WOWZAPP Windows 8 application as the example where it was successfully implemented. Today, I am going to talk how the data layer was implemented, that connected the app to Windows Azure Mobile Services. 

First of all - what is Azure Mobile Services? It is a system that allows you to easily store and retrieve data that is being frequently reused on mobile devices or applications that are targeting platforms such as Windows Phone or Windows 8, in the context of the Windows Store ecosystem.

To get free access to it, you need to register by using the Windows Azure Management Portal.

Once you create an account, you will need to download the appropriate SDK . Since WOWZAPP is a Windows 8 application, the Windows 8 SDK is being used. Once installed and ready, make sure you add a reference to the Windows Azure Mobile Services Managed Client:

In App.xaml.cs, right after the default class constructor, insert this snippet:

public static MobileServiceClient MobileService = new MobileServiceClient( "https://PROJECT_NAME.azure-mobile.net/", "API_KEY" );
This will be the primary entry point for the Azure Mobile Services content. Also, as there is no initial data to work with, make sure you enable Dynamic Schema in the Configure tab in the management portal:

This will allow you to easily create new tables as well as modifying their structure dynamically by simply serializing the associated models. When the application goes into production, this option should be disabled for security reasons.

So how is data inserted? Let's begin with a simple model - the one that is used for in-app screenshots. The class itself looks like this:
public class Screenshot : BindableBase
{

    private int _id;
    public int Id 
    {
        get { return _id; }
        set { this.SetProperty<int>(ref _id, value, "Id"); }
    }

    private string _imageUri;
    public string ImageUri
    {
        get { return _imageUri; }
        set { this.SetProperty<string>(ref _imageUri, value, "ImageUri"); }
    }
}
The class only contains basic data types and is automatically serializable. If you want to pass a model to a AMS data store, you need to make sure that it can be serialized properly.

Once a new instance of Screenshot is created, adding it to the storage is as simple as this:
await App.MobileService.GetTable<Screenshot>().InsertAsync(screenshot);
With Dynamic Schema on, the table will be automatically created based on the model layout. When it comes to getting the data out, the approach is very similar. Take this example, using the WorldEvent model:

var eventTable =  App.MobileService.GetTable<WorldEvent>();
MobileServiceTableQuery<WorldEvent> query = eventTable.Take(1000);
var collection = await query.ToListAsync();

CoreViewModel.Instance.WorldEvents = new System.Collections.ObjectModel.ObservableCollection<WorldEvent>(collection);
Notice one interesting thing - I am actually able to run LINQ queries against the table that is bound to a given model. With this infrastructure in place, it is really easy to extract and manipulate data with minimal additions to the application code.

Every single piece of data in the WOWZAPP application for Windows 8 relies on Windows Azure Mobile Services.