سؤال

I am playing around with Azure Mobile Services. Right now I am trying to store an object of my custom class in a table.

Here is a snippet from the class which represent the object which I want to store in Azure.

public class CustomItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Id { get; set; }

    [JsonProperty(PropertyName = "Categorie")]
    public CategorieObject Categorie { get; set; }

    [JsonProperty(PropertyName = "Subcategorie")]
    public SubcategorieObject Subcategorie { get; set; }

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

...

}

My question is how to store custom types like CategorieObject or SubCategorieObject. These classes contain a String for the name and many other properties, but I only need to store the name of the Categorie and SubcategorieObject. Maybe you can give me a hint, to solve my problem.

Thanks!

هل كانت مفيدة؟

المحلول

The post at http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/06/supporting-arbitrary-types-in-azure-mobile-services-managed-client-complex-types.aspx shows one way to support complex objects in azure mobile service. For your scenario, you can send the data to the server and in the insert/read/update scripts you "change" the data to only store what you need. For example, assuming that you have those types on the client:

public class CustomItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Id { get; set; }

    [JsonProperty(PropertyName = "Categorie")]
    public CategorieObject Categorie { get; set; }

    [JsonProperty(PropertyName = "Subcategorie")]
    public SubcategorieObject Subcategorie { get; set; }

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

public class CategorieObject
{
    [JsonProperty(PropertyName = "Name")]
    public string Name { get; set; }

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

public class SubcategorieObject
{
    [JsonProperty(PropertyName = "Name")]
    public string Name { get; set; }

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

You'd change your insert script to replace the complex object (categorie / subcategorie) with the name, which is what you want to store:

function insert(item, user, request) {
    // Replace the complex object with their "Name" property.
    // Omitting error / format checking here for conciseness.
    item.Categorie = item.Categorie.Name;
    item.Subcategorie = item.Subcategorie.Name;

    request.execute({
        success: function() {
            // Recreate the original object
            item.Categorie = { Name: item.Categorie };
            item.Subcategorie = { Name: item.Subcategorie };
            request.respond();
        }
    });
}

Likewise when updating an item you'd need to do the same:

function update(item, user, request) {
    // Replace the complex object with their "Name" property.
    // Omitting error / format checking here for conciseness.
    item.Categorie = item.Categorie.Name;
    item.Subcategorie = item.Subcategorie.Name;

    request.execute({
        success: function() {
            // Recreate the original object
            item.Categorie = { Name: item.Categorie };
            item.Subcategorie = { Name: item.Subcategorie };
            request.respond();
        }
    });
}

And the same for reading:

function read(query, user, request) {
    request.execute({
        success: function(results) {
            results.forEach(function(item) {
                item.Categorie = { Name: item.Categorie };
                item.Subcategorie = { Name: item.Subcategorie };
            });
            request.respond();
        }
    });
}

Notice that any other properties in the subclasses will be lost when reading from the database (after all, you're not storing them).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top