c# - Join DbSets with repository pattern -
i have basic implementation of repository pattern generic irepository<t> holds add,remove,find, etc. , have more precise repositories such iactorrepository , ifilmrepository derive irepository. have method search box finds top 5 actors , films match text search box.
my question how can join these repositories? need method testable, don't want iqueryable in method, interface can mock later on.
the thing me comes mind create kind of helper class such public class filmsactorshelper: ifilmsactorshelper. i'm sure had issue that, common practice here, how solved?
method need solution:
[httppost] public actionresult searchboxchanged(string inputvalue) { if (inputvalue == null || inputvalue.length < 3) return json(new list<string>()); const int maxnamescount = 5; var names = _filmrepository.find(f => f.name.contains(inputvalue)).select(f => f.name).take(maxnamescount).tolist(); var remaining = maxnamescount - names.count; var actornames = _actorrepository.find(f => f.name.contains(inputvalue)).select(f => f.name).take(remaining).tolist(); names.addrange(actornames); return json(names); } also, because have tolist() suspect pull database, right?
do not wrap type implements repository pattern (dbcontext) in own repository type(s). adding unneeded abstraction makes code more difficult read , have jump through hoops simple things joining tables. create service instead has instance of dbcontext , work in there , return result. consume service controller. can join on whatever want.
services should encapsulate , expose business logic. if want return list of films based on actor have method task<list<film>> getfilmsforactorasync(string actor) that. not try wrap each entity in own service, have added nothing of real value.
you can split services based on functionality , read/write. see cqrs pattern.
Comments
Post a Comment