c# - Awkward Generic Repository Call -
i implementing generic repository pattern
, have done quite few times every time there bugging me.
if have database design below. (all tables relate 1 another.) , make call following
public ienumerable<domain> getrealestate() { return _repository.getall(); }
i can models 1 call (the wonders of ef
). thing bugs me fact have domain
in method call, domain entity relevant entity (lazy loading
) companies
etc. etc. feels wrong use domain entity
companies etc. etc. repo pattern using straight forward one.
is there better way of writing methods not weird?
below sample code have
controller
[routeprefix("api/realestate")] public class realestatecontroller : apicontroller { private readonly irealestateservice _service; public realestatecontroller(irealestateservice service) { _service = service; } [route("")] public task<domain> getrealestates() { var collection = _service.getrealestate(); return null; } [route("{domainname}")] public task<domain> getrealestate(string domainname) { } }
service
public class realestateservice : irealestateservice { private readonly irealestaterepository _repository; public realestateservice(irealestaterepository repository) { _repository = repository; } public ienumerable<domain> getrealestate() { return _repository.getall(); } }
sense & responsibility.... dilemma see more people struggle with. think key feeling easier domain
can considered aggregate root encapsulates whole object graph it's owner of.
let's @ common example that's closer might think, dbset
. dbset
repository. think of think it's totally acceptable, practice, write query like
context.domain.include(d => d.companies)....
nobody say, has dbset<domain>
company
? when add domain
dbset
, , doing add entire object graph, nobody feel inclination add each object through "proper" dbset
. totally useless hell of job.
if wasn't practice, ef (or orm) wouldn't have navigation properties. think of that... nightmare.
Comments
Post a Comment