asp.net mvc - MVC sharing DbContext between repositories -
i'm developing course, department, student mvc application part of learning mvc.
i've gone multi-tier design, i.e. web, bll, dal.
my dal has dbcontext , number of repositories. e.g. student, department, course, search, audit...
next looking how share dbcontext among dal repositories led me read alot on design patterns, , ended @ generic repository, , unit of work (and quick @ dependency injection).
i discovered benefits of generic repositoy , uow. work great student, department, course... entities fit crud functionality. haven't coded yet.
but when came fitting search , audit generic repository , uow, wouldn't fit. background: why share dbcontext between search , audit? i'd audit each search within same transaction.
so stuck! because search functionality read-only no commits. , secondly both search , audit entities different - different each other, , not crud-like. generic repository not work here these 2 entities, far can see.
is there way share dbcontext between 2 different entities, e.g. search , audit?
the best approach can come put audit functionality within search dal repository. don't mixing these 2 entities, sounds ugly, guess can make exception given audit functionality.
the disadvantage of generic repository not entities support crud operations. repository doesn't have crud. can create entity specific repositories following.
public class search { public search(dbcontext dbcontext); } public class auditrepository { public auditrepository(dbcontext dbcontext); } a lot of developers not generic(crud) repository following reason:
"not every entity can deleted, not every entity can added, not every entity has repository" source
example repository has getbyid. requirements forbids create/update/delete.
public class userrepository { public user getbyid(int id); { } }
Comments
Post a Comment