1

All the sample applications and code that I have come across for consuming Azure mobile services don't follow MVVM pattern for simplicity.

How would one write an MVVM application that uses Azure mobile services for accessing data in cloud and then cache the data in Windows phone Local Database (model). My existing Model classes were like this -

[Table]
public class ToDoItem
{

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int ToDoItemId
    {
        ...
    }

    [Column]
    public string ItemName
    {
        ...
    }

    [Column]
    public bool IsComplete
    {
        ...
    }
}

And now that I want to work with this data in cloud, the samples tell me that I need to structure my classes like this -

public class TodoItem
{
    public string Id { get; set; }

    [JsonProperty(PropertyName = "text")]
    public string Text { get; set; }

    [JsonProperty(PropertyName = "complete")]
    public bool Complete { get; set; }
}

How does this fit in the MVVM pattern? What do my model classes need to look like. Do I use both versions of ToDoItem classes one for setting/getting data from local database and the other for setting/getting data from the cloud and something to convert one to the other? Is there a sample someone can point me to?

1 Answers1

0

First of all, you seem to be confusing Model and Databases here. Databases are just storage means. Model, on the other hand, is a representation of structure of your data. With that out of way, let's get to your problem.

You should not have two different models for the same data. It may lead to inconsistencies in future and it's bad design in general. You can totally have different class decorations like this:

[Table]
public class ToDoItem
{
    private int _toDoItemId;

    [JsonProperty(PropertyName = "id")]
    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int ToDoItemId
    {
        get
        {
            return _toDoItemId;
        }
        set { blah; blah; blah; }
    }
}

Here, I have used both types of attributes, Column as well as JsonProperty on the same member ToDoItemId. When data is serialized for database storage, the Column attribute will be taken into account, while Azure SDK will conveniently ignore it and use JsonProperty instead.

Here, I have written a library that handles synchronization between Windows Phone local database and WAMS. Even if you choose not to use it, you can browse the code and get some hints. Hope that helps!

akshay2000
  • 823
  • 8
  • 28