Build an Open Data App – Part 1: Dataset and DataModel

Over the next few posts I’m going to walk through the process of building a simple app for Windows Phone and Windows 8.1 using C# and XAML. In order to facilitate this, the dataset will be Open Data. To allow for a few interesting highlights in later posts, I’ll be using the Toronto Bike Share dataset, generously made available by the City of Toronto. Please be aware that the terms and conditions of Toronto’s Open Data usage is attribution to the city (check!) and a link to the license (check!). This is also required in the product (I’m not releasing this app, so I won’t be including it).

For this example app I’ll be making use of a Universal App (Windows Phone 8.1 and Windows 8.1), and since I do not plan on using the code in other apps I’ll not be using a Portable Class Library (PCL). If, however, you want to re-use your code in separate apps, I really can’t praise PCLs enough.

Data Model

To start, we’ll build our data model. Since we’ll want to use this in both the phone and PC apps, we’ll put it in our shared project.

image

You’ll notice as well, that I’ve added a few objects already. It’s a good idea to hold your strings in constants so you can quickly change them once (rather than at every usage). As for those two URLs, you technically only need one, but I want show the difference between using JSON and XML, so I thought I’d add both. Our BikeStation class will hold the data from the dataset, and you’ll notice I’ve also included a public property. You’ll want to have that later to get your data to the UI.

To construct the BikeStation class we have two choices: look at the dataset and copy-paste over field names, or we can do it the way I like and use Web Essentials. As I mention in the linked post, using web essentials makes for a faster, error free conversion. Of course, when you use the Paste Special feature you’ll find that the naming isn’t necessarily how you’d like it. Just use the Rename feature (select the class and hit F2 or your preferred access point) and fix your classes. Don’t do this to the fields, not yet at least. However, if you don’t like that Array, or would prefer to work in strings rather than ints, feel free to fix those now.

image

Why not change the field names, you ask? Nothing is stopping you if you plan on parsing the JSON or XML by hand and manually pushing the values to the fields, but I intend to be a little more efficient. As I’m building this example with JSON, I’m going to use Json.NET (available through Visual Studio via Nuget). This way all of the parsing is handled for me. If you’re going the Universal App route, be sure to add Json.NET to both the Windows and Windows Phone projects or you’ll have problems later on.

image

In the screenshot you can see that the old property names have been replaced with names that match my naming convention, and I’ve used JsonProperty to identify them with the value as it comes from the dataset. This makes sure Json.NET will populate the correct fields. Further, when I serialize the fields they’ll be serialized with the JsonProperty value. You’ll notice the JsonIgnore, however. The serializer with Json.NET will take any public fields and try to serialize them. Since this field is always derived I don’t want to serialize it (why waste the space?). But, as I need it public, I can use JsonIgnore to avoid this issue. If the property were private I’d not need to worry as the serializer would ignore it.

In the next stop on our Build an App journey, I’ll show you how to access the dataset online and parse it into the class you’ve now built.

This entry was posted in .NET, Building an Open Data App, Open Data, Windows 8, Windows 8.x, Windows Phone. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s