How should LINQ queries and Dao layer return queries to the view? -


when used use ado.net, used create tableadapters populated using joined queries, example product table adapter have column 'location' if underlying select query joined product location table.

in linq, how accomplish this? return columns in joined query writing linq queries in productsdao class? type returning binding gridview? problem retuning linq results have thought break consistency of productsdao class (this should return product rows db).

i think best option define classes represent joined data want return (e.g. productdetails) , return iqueryable<productdetails> data layer.

to return queryable, can write simple linq query:

public iqueryable<productdetails> getproducts() {   return p in db.products          join d in db.details on p.id equals d.id          select new productdetails { p.name, d.notes }; } 

this approach has several benefits:

  • you're not exposing yourdatacontext object directly - can stay private field of dao class (if short lived) or can created need it.

  • you're not exposing underlying linq entities directly (e.g. product or details), users cannot accidentally mess database (unless give them way that)

  • you're returning queryable, represents query. when users add additional constrains (e.g. where), query composed , won't loading unnecessary data.

  • you can expose reasonable functionality class - example, if have method getproducts taking categoryid, users can fetch products in given category (which makes impossible fetch entire table database)


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -