rest - Web API oData - Should I forget about view models? -


ive been working mvc while , used creating view model class every mvc view. trying out web api , think may hung on mvc mentality. relationship looks this:

public class supplier {     public int id { get; set; }     public string title { get; set; }      public virtual icollection<supplierproduct> supplierproducts { get; set; } }  public class product {     public int id { get; set; }     public string title { get; set; }      public virtual icollection<supplierproduct> supplierproducts { get; set; } }  public class supplierproduct {     public int id { get; set; }     public string title { get; set; }      public int supplierid { get; set; }     public virtual supplier supplier { get; set; }      public int productid { get; set; }     public virtual product product { get; set; } } 

i working on creation of supplier in create form user able select multiple products exist. in mvc, post view model looks this:

public class suppliercreateviewmodel {     public string title { get; set; }     public icollection<productviewmodel> selectedproducts { get; set; } } 

and in controller first create new supplier create new supplierproduct each selectedproduct. implemented in web api in post action of supplier odata controller doesnt feel right. think instead, need change approach , client:

  1. scrap view model design. (there arent 'views' anymore anyway)
  2. have both supplier , supplierproduct controller post action on both.
  3. on save, send supplier create request post api/suppliers/.
  4. using id of supplier json in response, send multiple create requests post api/supplierproduct.

so questions are:

  1. am heading in right direction approach?
  2. instead of view models there different pattern should use? dto?
  3. with example given, forced send 1 - n requests that? feels wrong.

actually, depends on use-case. if api totally faced publicly, advice using dto's. if yourselve or internal team, stick odata ef models ( because quicker)

  • you can ( usual) give entire entity through api.

  • you can use viewmodel ( more dto's when using in api, it's same thing) , transform methods accordingly. can use automapper - transforms $filter query, example found here : web api queryable - how apply automapper?.

don't forget, api has lot of awesome advantages. odata uses batch , patch change entities. stick odata entites of time, that's personal choice.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -