c# - Putting DBContext scope to IService -
i have web app , windows service app.
the web app injects ipersonservice
mvc controllers.
the windows app uses ipersonservice
.
the service takes 3 dependencies on ipersonrepo, iaddressrepo, iemploymentrepo
example.
the implementations of repositories take dbcontext
entity framework use.
in web app can register dbcontext bind<mycontext>().toself().inrequestscope();
in windows service trickier. leave dbcontext transient seems wrong.
so thought make services scope determine life cycyle of dbcontext unsure how go make sure worked web app , windows service app.
any appreciated
if it's important 3 repos use same dbcontext instance, can this:
var context = new dbcontext(...); bind<ipersonrepo>().to<personrepo>().withconstructorargument("dbcontext", context); bind<iaddressrepo>().to<addressrepo>().withconstructorargument("dbcontext", context); bind<iemploymentrepo>().to<employmentrepo>().withconstructorargument("dbcontext", context);
like same context instance shared between repos.
if repositories not aware of each other's entities (and changes these entities), inject fresh instance of dbcontext in each repo, binding in transient scope (default behavior):
bind<mycontext>().toself();
Comments
Post a Comment