Entity Framework 5 Get All method -
in our ef implementation brand new project, have getall method in repository. but, our application grows, going have let's 5000 products, getting way is, performance problem. wouldn't ?
if so, implementation tackle future problem?
any hightly appreciated.
it depends on how repository class set up. if you're performing query immediately, i.e. if getall()
method returns ienumerable<t>
or ilist<t>
, yes, performance problem, , should avoid sort of thing unless want load records @ once.
on other hand, if getall()
method returns iqueryable<t>
, there may not problem @ all, depending on whether trust people writing queries. returning iqueryable<t>
allow callers further refine search criteria before sql code generated. performance-wise, problem if developers using code didn't apply filters before executing query. if trust them enough give them enough rope hang (and potentially take database performance down them), returning iqueryable<t>
might enough.
if don't trust them, then, others have pointed out, limit number of records returned query using skip()
, take()
extension methods implement simple pagination, note it's possible records slip through cracks if people make changes database before move on next page. making pagination work seamlessly ever-changing database harder lot of people think.
another approach replace getall()
method 1 requires caller apply filter before returning results:
public iqueryable<t> getmatching<t>(expression<func<t, bool>> filter) { // replace myquery context.set<t>() or whatever you're using data access return myquery.where(filter); }
and use var results = getmatching(x => x.name == "foo");
, or whatever want do. note bypassed calling getmatching(x => true)
, @ least makes intention clear. combine first method put firm cap on number of records returned.
my personal feeling, though, of these ways of limiting queries insurance against bad developers getting hands on application, , if have bad developers working on project, they'll find way cause problems no matter try do. vote return iqueryable<t>
, trust used responsibly. if people abuse it, take away getall()
method , give them training-wheels methods getrecentposts(int count)
or getpostswithtag(string tag, int count)
or that, query logic out of hands.
Comments
Post a Comment