Cloud Zone is brought to you in partnership with:

G. Andrew Duthie, aka devhammer, is a Technical Evangelist for Microsoft's Mid-Atlantic States district, where he provides support and education for developers working with the Microsoft development platform. In addition to his work with Microsoft, Andrew is the author of several books on ASP.NET and web development, and has spoken at numerous industry conferences from VSLive! and ASP.NET Connections, to Microsoft's Professional Developer Conference (PDC) and Tech-Ed. G. Andrew is a DZone MVB and is not an employee of DZone and has posted 40 posts at DZone. You can read more from them at their website. View Full User Profile

Back-end Data Services for Windows 8 Apps: Azure Mobile Services

12.18.2012
| 2567 views |
  • submit to reddit

If you've been following along with this series so far, you know we've come along way, from my overview of the series, which described the target scenario of a simple leaderboard service for a couple of JavaScript-based Windows 8 games, to the 2-part post (Part 1 / Part 2) on building this service using WCF Data Services, and hosting as an Azure Cloud Service, to the most recent installment, in which I showed how to build the same service using ASP.NET Web API, and hosted in a Windows Azure Web Site.

To round out the group of solutions I'm exploring in the series, in this post I'll show you how to build the same game leaderboard service on top of the new Windows Azure Mobile Services (in preview as of this writing).

A Different Approach

In both of the previous versions of the leaderboard service, the solution involved a code-based server-side solution. On the plus side, in both cases, Visual Studio did most of the work of creating the code (along with the Entity Framework, WCF Data Services, and Web API libraries), but the end product is a project with source code that needs to be maintained.

By contrast, Windows Azure Mobile Services takes a simpler approach, that allows me as the developer to focus more on my client code, and less on the server side code.

Getting Started

As with my other examples, you'll need a Windows Azure account to follow along. If you have an MSDN Subscription, check your benefits page, as it may include some Windows Azure benefits. If you don't have an MSDN subscription, you can sign up for a 90-day trial that will give you everything you need to follow along, and by default includes a spending cap to ensure that you don't have any out-of-pocket expenses if your services exceed the trial service limits (if your services exceed the trial limits, they'll automatically be shut down).

With my Azure account in-hand, I start by signing into the Windows Azure Management Portal, which by default gives me an overview of all of the services I have running in Windows Azure, which includes all subscriptions that are managed by the account I use to log in, as you can see below:

ManageAllItems

One of the "tabs" along the left side is MOBILE SERVICES, which is just what I'm looking for, so I'll click that, which takes me to the mobile services home page. I've removed a couple of existing services I was working with, so there's not much to see yet:

MobileServices1

I'll get started by clicking the + NEW link at the bottom (clicking the link in the middle of the page would work, too), which gives me a nice Create option below (and shows neatly where Mobile Services fits in the Azure ecosystem):

MobileServices2_New

Clicking Create gives me the following 2-step wizard for creating the service (since I already created a database instance in an earlier part of this series, I'll go ahead and leverage that existing database instance):

MobileServices3_Create1

Note that the URL must be unique, and the wizard will check the name dynamically and let you know if your desired name is available. For the free version of Mobile Services, all URLs will use the pattern MYSERVICENAME.azure-mobile.net, but I can (at an additional cost) also configure my service to use a custom domain name, once the initial setup is complete. For my purposes, the existing domain name is fine, since end-users won't be interacting with the service directly.

Clicking the arrow icon takes me to the second page of the wizard, where I will provide my database credentials:

MobileServices3_Create2

Clicking the checkmark icon will start the process of creating the service, which takes a few minutes. When the service has been set up, I get a screen that looks like the following:

MobileServices4_Created

NOTE: Please be aware that while the service URL above is public, I may or may not be keeping it active after this post is complete, so it may not be accessible. If you want to experiment with mobile services, I encourage you to create your own service.

What Have I Done?

OK, so now I have my mobile service. But what does that mean, exactly?

Basically, in a few short steps, I have (with the help of Azure) built out an infrastructure that will provide me with:

  • A place to store data
  • The ability to create tables and schemas (schemas are dynamic by default…more on that later)
  • RESTful services to access the data in any tables I create
  • Client libraries to simplify the process of interacting with the back-end data tables
  • Push notification services
  • Authentication providers

I'll cover the last two features in future posts, but the rest of what we get is pretty cool. Essentially, when I create a new table (I'll walk through how I do that shortly), Windows Azure Mobile Services automatically creates RESTful services that provide basic Create, Read, Update, and Delete functionality for that table. And while mobile services also provides client libraries for several client platforms (with more on the way), the underlying services are accessible via simple HTTP calls, so they're open to any client platform that can make HTTP calls.

And for folks who like transparency, the client SDK for Mobile Services is being developed on GitHub, so you can check out the code, fork it, and contribute bug fixes and features as well.

Getting a Quick Start

Now that my mobile service is created, I can simply click the name of the service to start working with it. By default, the first screen I'll see is the Quick Start page, shown below (note the checkbox, which allows me to skip the Quick Start next time):

MobileServices5

The Quick Start screen is designed to get users up and running fast with their mobile services, and (as of this writing) supports 3 client platforms out of the box: Windows Store (C# or JavaScript apps), Windows Phone 8, and iOS. Android support has been announced as well.

For each of these client platforms, the Quick Start page provides a tutorial on creating a new app for that platform using the new mobile service, or alternatively, for connecting an existing app to the mobile service. Since my goal is to connect this service to a couple of existing games, that's what I'll start with.

Note also that the Quick Start page provides links to instructions for adding authentication and push notifications for each platform (again, I'll show you how to leverage these features in future posts).

Since I'm going to use my service with a Windows Store app, I'll switch to the Windows Store plaftorm, then click the heading labeled "Connect an Existing Windows Store app", which gives me some simple instructions, as shown below:

MobileServices6

The first two steps are pretty straightforward, one, installing the Mobile Services SDK, and two, referencing the SDK in my Windows Store app project, and adding the initialization code to the project. The initialization code in step 2 provides the client library with the URL from which to access the service, plus the application key (obfuscated above), which is the default means of authenticating the app to the service and authorizing table access. The Data tab for a given service in the management portal allows more restrictive permissions to be put in place, but I'm going to stick with using the application key for the time being.

I've already got the Mobile Services SDK installed, so let me show you how simple it is to add a reference to the SDK. I'll open up my Windows Store app (in this case, the Space Cadet game I've been using as one of my demo apps throughout this series), and in Solution Explorer, right-click the References node (which you'll find right below the project node), and select Add Reference… Find the Windows Azure Mobile Services JavaScript Client, and check the checkbox to the left of it, as shown below:

AddReference

Then click OK to add the reference.

Step 3 in the Quick Start shows the syntax needed to add new items to an Item table, which I can create by clicking the Create Item table button. Alternately, if I want a table with my own name, I can go to the Data tab and add a new table there:

MobileServices7_DataTab

When I click the Add a Table link (or the Create link at the bottom of the page), I'm presented with a dialog that allows me to specify the name of the table, as well as the permissions for the Insert, Update, Delete, and Read permissions:

MobileServices8_CreateTable

As noted earlier, the default is to allow anyone with the application key to access these operations on the table. Additional permissions include Everyone, which opens the operation up to anyone who has the service URL (you might use this to provide open access to the read operation, but it would be pretty rare to use this for other operations), Only Authenticated Users, and Only Scripts and Admins. The latter permission means that the table can only be accessed in the management portal and via server-side scripts.

I'll set my table name to "gamescore," as shown above, and click the checkmark to create the table. Once the table creation is complete, if I click into the gamescore table, and switch to the Columns tab, I see that there's only one column defined, called id, and it's indexed, and of type bigint.

One thing you might notice is that there's no interface here to add columns. That's because by default, mobile services use a dynamic schema model, in which anything I add to the named table from my client code will automatically generate the necessary schema on the fly. This provides a very flexible mechanism for storing data, particularly if you don't know up-front what data you'll need to store.

Connecting the Client

At this point, I have everything I need on the server side to successfully connect my game to the leaderboard service. The next step is to wire up the client code, and as with the previous examples, I'll do this by creating a separate JavaScript file that contains all the logic for interacting with the service, and expose this via a defined API using the WinJS.Namespace.define function, just as I did in the previous examples in this series. Let's take a look at the full code, and I'll explain the key parts one by one:

leaderboardWAMS.js

  (function () {

      "use strict";

   

      // Leaderboard Client for Windows Azure Mobile Services

      // by G. Andrew Duthie

      // This code is copyright © Microsoft Corporation, and licensed under the Microsoft Limited Public License (Ms-LPL). 

      // All rights reserved. 

      // Code is provided AS-IS with no warranties.

   
      var leaderboardClient, gameScoresTable, leaderboardList, playerName, gameName, diag;
   
      WinJS.Namespace.define("Leaderboard", {
          setPlayerName: setPlayerName,
          init: init,
          addWin: incrementWins,
          addLoss: incrementLosses,
          addTie: incrementTies,
          updateScore: updateHighScore,
          getTopTenScores: getTopTenScores,
          getTopTenWins: getTopTenWinLossTie,
          leaderboardList: leaderboardList
      });
   
      // player and game are required for initialization
      function init(player, game) {
          return new WinJS.Promise(
              function (completed, error, progress) {
                  if (player) {
                      playerName = player;
                  }
                  else {
                      error("Player Name is required for initialization");
                  }
   
                  if (game) {
                      gameName = game;
                  }
                  else {
                      error("Game Name is required for initialization");
                  }
   
                  // replace with your custom WAMS instance information
                  leaderboardClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient(
                      "https://YOURAPPNAME.azure-mobile.net/",
                      "YOUR WINDOWS AZURE MOBILE SERVICE APPLICATION KEY"
                  );
                  // Replace "gamescore" with your table name, if different
                  gameScoresTable = leaderboardClient.getTable('gamescore');
                  // make sure that the score table has at least one record for the player name / game name combo
                  gameScoresTable.where({ player: playerName, game: gameName })
                      .read().done(
                      function (results) {
                          if (results.length == 0) {
                              // initialize table
                              var gameScore = {
                                  game: gameName, player: playerName, score: 0, wins: 0, losses: 0, ties: 0
                              };
                              gameScoresTable.insert(gameScore);
                          };
                          completed();
                      },
                      function (e) {
                          error(e);
                      });
              });
      }
   
   
      function getCurrentPlayerScore() {
          return new WinJS.Promise(
              function (completed, error, progress) {
                  gameScoresTable.where({ player: playerName, game: gameName })
                  .read().done(
                  function (results) {
                      completed(results[0]);
                  },
                  function (e) {
                      // handle exceptions
                      error(e);
                  });
              });
      }
   
      function setPlayerName(name) {
          var namePromise = getCurrentPlayerScore();
          namePromise.done(function (currentScore) {
              if (playerName != name) {
                  playerName = name;
                  currentScore.player = playerName;
                  gameScoresTable.update(currentScore);
              }
          }, function (e) {
              showMessage(e.message);
          });
      }
   
      function incrementWins() {
          var scorePromise = getCurrentPlayerScore();
          scorePromise.done(function (currentScore) {
              currentScore.wins++;
              gameScoresTable.update(currentScore);
          }, function (e) {
              showMessage(e.message);
          });
      }
   
      function incrementLosses() {
          var scorePromise = getCurrentPlayerScore();
          scorePromise.done(function (currentScore) {
              currentScore.losses++;
              gameScoresTable.update(currentScore);
          }, function (e) {
              showMessage(e.message);
          });
      }
   
      function incrementTies() {
          var scorePromise = getCurrentPlayerScore();
          scorePromise.done(function (currentScore) {
              currentScore.ties++;
              gameScoresTable.update(currentScore);
          }, function (e) {
              showMessage(e.message);
          });
      }
   
      function updateHighScore(newScore) {
          var scorePromise = getCurrentPlayerScore();
          scorePromise.done(function (currentScore) {
              if (currentScore.score <= newScore) {
                  currentScore.score = newScore;
                  gameScoresTable.update(currentScore).done(function () {
                      showMessage("Leaderboard Updated.");
                  });
              }
          }, function (e) {
              showMessage(e.message);
          });
      }
   
      function getTopTenScores() {
          return new WinJS.Promise(
              function (completed, error, progress) {
                  gameScoresTable
                  .select("player", "score")
                  .where({ game: gameName })
                  .orderByDescending("score")
                  .read().done(
                      function (results) {
                          leaderboardList = new WinJS.Binding.List(results);
                          completed(leaderboardList);
                      },
                  function (e) {
                      // handle exceptions
                      error(e);
                  });
              });
      }
   
      function getTopTenWinLossTie() {
          return new WinJS.Promise(
              function (completed, error, progress) {
                  gameScoresTable
                  .select("player", "wins", "losses", "ties")
                  .where({ game: gameName })
                  .orderByDescending("wins")
                  .read().done(
                      function (results) {
                          leaderboardList = new WinJS.Binding.List(results);
                          completed(leaderboardList);
                      },
                  function (e) {
                      // handle exceptions
                      error(e);
                  });
              });
      }
   
      function showMessage(msg) {
          diag = new Windows.UI.Popups.MessageDialog(msg);
          diag.showAsync();
      }
  })();

Here are the important parts of the code:

  • Lines 12-22: As with the other examples, this is where I'm defining the externally-facing API for calling the leaderboard code. Because all the code in the file is wrapped by an anonymous self-executing function, nothing that isn't explicitly exposed can be seen by the outside world.
  • Line 43: Part of the init function, this code initializes the leaderboardClient object, using the URL for the mobile service, along with the application key, using the constructor of the MobileServiceClient object exposed by the Windows Azure Mobile Services JavaScript Client library. Note that you need to supply your own specific URL and application key.
  • Line 48-64: In this code, also part of the init function, I get a reference to the gamescore table created earlier, and then query the table using intuitive .where and .read functions. The arguments I pass to the where function limit the data returned to just this game and player. Note the .done function, which indicates that .read returns a WinJS.Promise object, which makes sense, since this is an operation that should be asynchronous. When the .read function returns, I check to see if any records were returned. If not, then I create an object containing the basic schema for my gamescore record, and use the table object reference's .insert function to add it to the gamescore table, then call the completed function to let the caller of the init function know that we're done.

Once this part of my code has executed at least once, I'll see the following, if I look at the Columns tab for my gamescore table in the management portal:

MobileServices9_DataTab_Columns

So what happened here? Basically, the client library referenced by my app packaged up the data as JSON (which doesn't really require any work, since I defined it that way in the first place…but it's good to keep in mind that even if you're using the managed library in a C# app, the data is transported as JSON), and then called the appropriate REST URL to insert the new record in the table. Since the table did not have a schema defined, mobile services inspected the JSON data, and defined columns capable of supporting the data provided. This is pretty cool, since it allows me the flexibility of defining my schema on the fly in my code. The downside to be aware of, however, is that if you have a typo in your code, mobile services will just as happily create a column using the misspelled column name, which can lead to some fun debugging. For this reason, mobile services allow you to turn off the dynamic schema feature, if you choose. You may want to leave dynamic schema on initially, while you're defining your schema, then turn it off to avoid accidentally redefining the schema.

OK, back to the code:

  • Line 72: This code, similar to that in the init function, calls the table object's .where and .read functions to get unique records for this player and game (there should only be one record per player/game combo), and returns the resulting object to the calling function using the WinJS.Promise.done function.
  • Lines 84, 97, 107, and 117: In each of these functions, I call the getCurrentPlayerScore function, which returns the gamescore record for the current player/game as described above, and depending on what kind of game I'm playing, updates the gamescore table (in order) with a new player name, increments the wins column, increments the losses column, or increments the ties column. In each case, if something goes wrong, I call a helper function to display a message to the user.
  • Line 127: In the updateHighScore function, I again call getCurrentPlayerScore, then check to see if the score in the returned record is less than the new score for the just-finished game. If it is, then I set the score property of the currentScore object to the new score, and call the table object's .update function to send the updated data to the service. In this case, I also display a message on successful completion, leveraging the fact that the .update method also returns a promise.
  • Lines 141 and 160: These two functions, which are used by the leaderboard page, take advantage of some of the rich querying functions of the table object, which allow me to create somewhat LINQ-like queries to select just the columns I want for a given purpose (.select), limit the scope of the data returned (.where) and order the data (.orderByDescending). You can read more about the available functions here. Once I've got the results I need, I simply pass them into the constructor of the WinJS.Binding.List object, and pass the resulting list back to the caller, which will use it to bind the data into a ListView control on the leaderboard page.

That's really all there is to it, for my JavaScript file.

To make the final connection with the game itself, I simply need to update the script reference in my default.html and leaderboard.html pages, like so:

<script src="/js/leaderboardWAMS.js"></script>

I also need to reference the MobileServicesJavaScriptClient/MobileServices.js file as well (note that this reference should be BEFORE the reference to my script above):

<script src="/MobileServicesJavaScriptClient/MobileServices.js"></script>

In the case of the leaderboard page, I also need to make sure that the databinding properties in my template match the column names in my table, as these are case-sensitive (lines 4 and 7 below):

  <div id="myTemplate" data-win-control="WinJS.Binding.Template">
      <div>
          <div class="win-type-x-large" style="width: 400px;">
              <span>Player: </span><em><span data-win-bind="textContent: player"></span></em>
          </div>
          <div class="win-type-large">
              <span>Score: </span><em><span data-win-bind="textContent: score"></span></em>
          </div>
      </div>
  </div>

Because the functions exposed in my Leaderboard namespace are the same as in the previous examples, there's no need to update the game logic at all. Once I've updated the script reference, I can simply run the game, and when the game ends, if my score is higher than the existing score, I'll see the following:

screenshot_12052012_211304

And when I visit the leaderboard page, here's what I see:

screenshot_12132012_180458

Of course, since I've only just created my table, there's only one score record, but it's nice to see that it's working fine.

For good measure, after updating the code for the Catapult Wars game, and playing a couple of games, here's what my gamescore table data looks like in the management portal:

gamescore_final

Wrap-up

In this post, I've shown you how easy it is to get started with Windows Azure Mobile Services. Creating a new mobile service and table is fast and simple, and the client libraries provide a quick and intuitive way of wiring your apps up to your mobile service, without worrying about the underlying REST calls.

In fact, simply by switching my leaderboard service over to Windows Azure Mobile Services, I was able to reduce the size of my leaderboard JavaScript library by nearly 30%. That's a significant reduction in code, nearly all of which is due to the simpler code required thanks to the client library.

But the cool part is that it's all still just RESTful services under the covers. So if I was building an app on a platform for which Windows Azure Mobile Services does not yet provide a client library (for example, Android, or even a web site), as long as that platform can make HTTP calls, I can still leverage mobile services as a back-end. You can read all about the Windows Azure Mobile Services REST API here.

Here are the advantages and disadvantages, in my view, of using Windows Azure Mobile Services for this kind of service:

Advantages
  • Quick to Develop – Setting up a mobile service and data tables takes little time at all, and thanks to the client libraries and dynamic schema capability, you can easily have a basic service up and running and connected to your app in a matter of minutes. 
  • Integration – In addition to the back-end data and REST services explored in this post, mobile services can also provide authentication against a variety of identity providers (as of this writing, Microsoft Accounts, Google Accounts, Twitter, and Facebook), as well as send push notifications. I'll explore these features in future posts.
  • Query flexibility – The mobile services client libraries provide intuitive query functionality, making it easy to filter and sort data. As I showed above, the JavaScript client library does this via chainable function calls.
  • Scalable – Windows Azure Mobile Services lets you start off small (and inexpensive) with a Shared service mode, and in the preview period, you can have up to 10 services in shared mode for free, all running on a single SQL Database instance. This keeps costs low, but when demand for your service grows, you can move to Reserved mode, and have your service running multiple instances to support many users.
Disadvantages
  • Preview Service – At the time of this writing, Windows Azure Mobile Services is in preview mode. This means that parts of the platform are still in development, and there is no SLA offered for the preview period.
  • Limited support for server-side validation – One feature I did not have time to discuss in depth is mobile services support for server-side scripts (using JavaScript running in node.js). For each operation, on each table, you can have your mobile service execute JavaScript code. This is useful when you want to take specific actions. For example, when a new record is inserted in a table, you might want to send a push notification. You can also use these server-side functions to provide some basic input validation, but at the time of this writing this requires you to write all of the code for this validation. This is an area where some of the other technologies I discussed have an advantage, in that in many cases you can simply decorate your model classes with the appropriate metadata attributes, and client code will do the validation for you.

If you want to spend the majority of development time focused on the client end of things, Windows Azure Mobile Services is a platform you should definitely consider. Personally, I find it a great way to build a back-end for my app fast. By abstracting the underlying complexity of building a RESTful interface on top of data tables, and providing easy-to-use client libraries, mobile services helps you get your service up and running quickly, and without the learning curve of technologies like WCF Data Services or ASP.NET Web API.

Ultimately, which of these three technologies is best for you will depend on your experience, and where you want to focus your time.

For Review

If you haven't already, I highly recommend that you read the rest of the posts in this series:

What's Next?

In future installments of this series, I'll explore adding authentication and push notifications to my leaderboard service, using Windows Azure Mobile Services. I'll be taking some time off for the Christmas holiday, so expect the series to continue in early January.

While you're waiting, why not sign up for Generation App? There are lots of great resources available for building Windows 8 apps (and now for Windows Phone 8 as well), including new information on a variety of app frameworks from partners that make it fast and easy to build apps and games for Windows 8. It's free, and you control how often updates are sent, so there's no good reason to pass it up. Sign up now!



Published at DZone with permission of G. Andrew Duthie, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)