asp.net mvc 4 - Microsoft Odata api through a ViewModel has problems in PATCH -


objectives objective send (non-defined) properties entity product. example in an angularjs listing view, need show products links(accessible) , others not accessible based on permissions calculated current user (which session)' data , productid.

what forces me have problem

now, odata doesn't allow me add properties while sending iqueryable result this.

public iqueryable<product> get() {   return db.products.asqueryable<product>(); } 

simply because returning type product , adding properties make else, if try this

var result = db.products.select(p => new product {   id = p.id,   accessible = (code permission checks),   [other properties...] } 

solution approached

i approached solution, making odatacontroller of new type odataproduct has defined properties need send make list permission aware. (do have idea how make lists permissions aware, other solution?)

public class odataproduct {     public product product { get; set; }     public odataproduct(product product)     {         this.product = product;     }     //     public void setpermissions(user user, string controller)     {         if (user == null) return;         isallowed = [permission check code];     }     public bool isallowed { get; set; } } 

i tried inherit odataproduct form product faced problem in downcasting odataproduct product when receiving post requests save database. db.products.add();

now viewmodel , controller of type have sent permission aware results dependent on current user in session products in list this.

 public class odataproductcontroller : odatacontroller {     private offerassistantdbcontext db = new offerassistantdbcontext();      // api/odataproduct     public iqueryable<odataproduct> get()     {         var result = db.products.asqueryable<product>();         var oresult = new list<odataproduct>();         var currentuser = (user)httpcontext.current.session["loggeduser"];         odataproduct oproduct;         foreach (var item in result)         {             oproduct = new odataproduct(item);             oproduct.setpermissions(currentuser, "product");             oresult.add(oproduct);         }          return oresult.asqueryable<odataproduct>();    }    //other methods of controller below... } 

problem want solution for

  1. when send patch request odataproduct controller, delta object not product, if send product in payload , modify accordingly odata parameters of patch method receive product instead of odataproduct received null, while in default case not able run command patch real entity of product not viewmodel. below.

    var dbproduct = db.products.find(key); odataproduct.patch((dbproduct); //odataproduct not of dbproduct type solution?

  2. another problem face while setting permissions of odataproduct above

    oproduct.setpermissions(currentuser, "product"); //stepping thie method exception line this.childs = product.dependantproducts.where(dp => dp.parentid == product.id).count();

it says datareader open. though not main problem please give info here too.

the first problem can solved way, firstly, define view moodel odataproduct contains needed properties of model plus isallowed:

public class odataproduct {     public int id { get; set; }     public string title { get; set; }     public bool isallowed { get; set; } } 

then create new entityset element type odataproduct.

private static iedmmodel getmodel() {     odatamodelbuilder modelbuilder = new odataconventionmodelbuilder();     var odataproductsentityset = modelbuilder.entityset<odataproduct>("odataproducts");     return modelbuilder.getedmmodel(); } 

the last step update odataproductcontroller execute get, post, patch ect.

public class odataproductscontroller : odatacontroller {     private productscontext db = new productscontext();      public ihttpactionresult get()     {         var products = db.products.select(m => new odataproduct()         {             id = m.id,             title=m.title,             // other properties             isallowed = true,         });         return ok( products);     }      public odataproduct patch(int key, delta<odataproduct> odataproduct)     {         var dbproduct = db.products.single(m => m.id == key);         foreach (string propertyname in odataproduct.getchangedpropertynames())         {             if ("isallowed" == propertyname)             {                 continue;             }             object propertyvalue;             if (odataproduct.trygetpropertyvalue(propertyname, out propertyvalue))             {                 var propertyinfo = typeof(product).getproperty(propertyname);                 propertyinfo.setvalue(dbproduct, propertyvalue);             }         }         db.savechanges();         odataproduct odataproductreturn = new odataproduct()         {             id = dbproduct.id,             title=dbproduct.title,             // other properties         };         odataproductreturn.isallowed = true;// or false according business logic         return odataproductreturn;     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -