.net - RESTful and Repository returning values from another type architecture -


in short, have 2 database tables: languages , frameworks. important thing there one-to-many relation between tables(one language has many frameworks). designing restful(webapi2) service consume information these tables. using repository pattern. using ef, i've made navigational property language reach frameworks.

however, how should implemented in repositories. correct return framework collection in language repository? want reach frameworks of language using same webapi controller because gives more simple route , i'm not sure if correct way @ all.

public class languagescontroller : apicontroller {     private readonly iprogramminglanguagerepository languages;      public languagescontroller() : this(new programminglanguagerepository(new cvsystemdbcontext()))     {     }      public languagescontroller(programminglanguagerepository languagesrepository)     {         this.languages = languagesrepository;     }      [httpget]     [route("api/languages")]     public ihttpactionresult getall()     {         return this.ok(this.languages.getall());     }      [httpget]     [route("api/languages/{id:int}")]     public ihttpactionresult getbyid(int id)     {         return this.ok(this.languages.getbyid(id));     }      [httpget]     [route("api/languages/{id:int}/frameworks")]     public ihttpactionresult getbylanguage(int id)     {         ----     } } 

in other hand, if implement in framework repository(either using navigation property other table in context or scanning id), should use nasty routing in framework webapi controller(something "api/frameworks/bylanguage/{id}") again doesn't seem right.

in opinion, restful service clean possible best approach define api controllers on business entity basis. is, should define controller crud operations on languages , frameworks separately, so:

public class languagescontroller : apicontroller {     public ihttpactionresult get(int id)     {         // logic query , return language id     }      public ihttpactionresult getall()     {         // logic query (and possibly paginate) languages     } }  public class frameworkscontroller : apicontroller {     public ihttpactionresult get(int id)     {         // logic query , return framework id     }      public ihttpactionresult getbylanguage(int id)     {         // logic return frameworks of specific language     }      public ihttpactionresult getall()     {         // logic query (and possibly paginate) frameworks     } } 

the point here restful service, in opinion, best not assume how data returned them consumed. is, should not assume everywhere languages displayed frameworks displayed well. example, imagine client lists languages in tabular manner. make pretty messy ui include frameworks there well. better let user pick language , display details separately, example, in modal window. whole point of restful service is, afterall, supply data granually possible , not tailor specific views. meant serve data kinds of http-capable devices , software - including browsers , smartphone apps. if want serve data tailored views using ajax calls, better define action methods on regular mvc controllers respond ajax calls , return jsonresult.

also 1 thing i'd point out here should not return db entites in services. i'll mention two+one reasons this:

  • security: returning db entities without control means every time modify data model, these changes reflected in data sent clients. because serializer transforms data demanded format not aware of data include - unless add attributes it, shouldn't in case because project contains entities should not rely on web api-specific assemblies. if modify entites of them contains sort of sensitive data, data included in response well. might thing not worry in specific domain, should keep in mind.
  • domain traversal: serializer transforms data - unless instructed otherwise using attributes problematic explained in previous bulletpoint - transforms every property finds demanded format, json, recursively. generally, have way more entites described in question - users (containing sensitive data, such password hashes), administrative data , whatever can think of. uncommon data model not connected - is, entity can reached other entity traversing 1 or more navigation property. if serializer not told data include , transform, recursively includes connected entites. because of data model being connected, means every record of every table gets included , leads stack overflow exception or large response message.
  • security+domain traversal combined: first , second issue implies if db has few enough data fit in single response , serialized, sensitive data (for example, user data, password hashes etc) included without realizing this.

in short, solve problem should define classes describe data send client , implement logic of how select them.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -