json - Azure Mobile Services (Windows Phone) - store complex object -
i playing around azure mobile services. right trying store object of custom class in table.
here snippet class represent object want 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 how store custom types categorieobject or subcategorieobject. these classes contain string name , many other properties, need store name of categorie , subcategorieobject. maybe can give me hint, solve problem.
thanks!
the post @ http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/06/supporting-arbitrary-types-in-azure-mobile-services-managed-client-complex-types.aspx shows 1 way support complex objects in azure mobile service. scenario, can send data server , in insert/read/update scripts "change" data store need. example, assuming have types on 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 insert script replace complex object (categorie / subcategorie) name, want store:
function insert(item, user, request) { // replace complex object "name" property. // omitting error / format checking here conciseness. item.categorie = item.categorie.name; item.subcategorie = item.subcategorie.name; request.execute({ success: function() { // recreate original object item.categorie = { name: item.categorie }; item.subcategorie = { name: item.subcategorie }; request.respond(); } }); }
likewise when updating item you'd need same:
function update(item, user, request) { // replace complex object "name" property. // omitting error / format checking here conciseness. item.categorie = item.categorie.name; item.subcategorie = item.subcategorie.name; request.execute({ success: function() { // recreate original object item.categorie = { name: item.categorie }; item.subcategorie = { name: item.subcategorie }; request.respond(); } }); }
and same 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 other properties in subclasses lost when reading database (after all, you're not storing them).
Comments
Post a Comment