repository pattern - mvc: same query for different repositories? -
my friends, me in bad situations :) thank you.
have 2 or more repositories working in own contexts return data joining same base query: public iqueryable<lq_group> getadminedgroupsq(user user) result used user, group, groupmember , others reposotories. best way implement that? dont idea have different copies of same logic. thoughts?
{
try
{
return
(from ap in context.lq_permissions
join g in context.lq_groups on
new { groupid = ap.objectid, grouptypeid = ap.objecttypeid }
equals
new { groupid = g.groupid, grouptypeid = dict.objecttype[objecttype.group].id }
((ap.subjectid == user.id) && (ap.subjecttypeid == dict.objecttype[objecttype.user].id))
select g);
}
catch (exception ex)
{
throw new unknownrepositoryexception(ex.message);
}
}
thank you.
you this:
public class myrepository { private list<item> mitems; public ienumerable<item> getallitems() { return this.mitems; } public ienumerable<item> getgooditems() { return this.getallitems().where(x => x.isgood == true); } public ienumerable<item> getgooditemsoverfivedollars() { return this.getgooditems().where(x => x.price > 5.00m); } } public class item { public bool isgood { get; set; } public decimal price { get; set; } } you can because query not executed until enumeration evaluated. however, keep in mind additional linq statements can added outside myrepository class, , can have "linq injection" ;).
you avoid wrapping methods , making them internal, wrapping methods call tolist or toarray, allowing query execute.
how executing queries, , repetition trying reduce? can provide example?
Comments
Post a Comment