c# - Having both an InRequestScope and InTransientScope for Ninject resolving to same type -


i've got ninject setup creates jobcontext resolver inrequestscope() works fine, however, have specific call in website requires me loop through few databases (all data in databases year). couldn't quite figure out going on because had forgotten jobcontext inrequestscope last block of code not acting how thought should.

here setup

//ninject module bind<data.ijobcontext>().to<data.jobcontext>().inrequestscope();   //controller's initialize protected override void initialize(system.web.routing.requestcontext requestcontext) {     base.initialize(requestcontext);      //set connection string jobcontext     this.jobcontext = dependencyresolver.current.getservice<ijobcontext>();     jobcontext.setyear(currentyear); } 

since jobcontext in request scope keeps reusing same object each year. instance need intransientscope rather inrequestscope.

//special function foreach (int year in activeyears) {     jobcontext = dependencyresolver.current.getservice<ijobcontext>();     jobcontext.setyear(year);     dosomething(); } 

how can accomplish this?

one question comes if need jobcontext in request scope , in other cases in transient scope. there seems design smell! try fix before doing the following.

if want way described have specify 2 different named bindings 1 in transient , 1 in request scope , them them name.

this.bind<ijobcontext>().to<jobcontext>().inrequestscope().named("requestscoped"); this.bind<ijobcontext>().to<jobcontext>().intransientscope().named("transientscoped"); kernel.get<ijobcontext>("requestscoped"); 

just thing: i'd succest try rid of servicelocator kind usage of ninject kernel , use dependency injection instead. i'll better design.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -