Using Portable class Library to hold your Azure Mobile Services model

If you want to use your model classes in other projects (like for example your upcoming Windows Phone 8 app) you can define them in a Portable Class Library.

Normally when using Azure Mobile Services we use the DataTable attribute on classes to denote their table name when stored in the Azure database.

But if you want to define the model classes from PCL using the DataTable attribute isn’t possible, as the namespace Microsoft.WindowsAzure.MobileServices isn’t available in a PCL.

using System.Runtime.Serialization;
using Microsoft.WindowsAzure.MobileServices;

namespace Demo
{
    [DataTable(Name = "users")]
    public class User
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "user_name")]
        public string UserName { get; set; }

        [DataMember(Name = "password")]
        public string Password { get; set; }

        [DataMember(Name = "user_level")]
        public int UserLevel { get; set; }
    }
}

The solution here is to use the DataContract attribute from System.Runtime.Serialization. Make sure though that you got the latest version of the Azure Mobile Services SDK or might not work!

using System.Runtime.Serialization;

namespace Demo
{
    [DataContract(Name = "users")]
    public class User
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "user_name")]
        public string UserName { get; set; }

        [DataMember(Name = "password")]
        public string Password { get; set; }

        [DataMember(Name = "user_level")]
        public int UserLevel { get; set; }

    }
}

For a download of the Azure Mobile Services SDK as a source, visit the GitHub page.

Or use this link for a straight msi download.

Using Portable Class Library for Azure Mobile Services model

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.