c# - Configure Ninject Factory to return the same instance every time -


i have service layer reusing (aka business layer).

here's example of 1 of services imycontextfactory dependency, returns instance of imycontext.

public class myservice : imyservice  {     private imycontextfactory dbcontextfactory;      public myservice(imycontextfactory dbcontextfactory)     {         this.dbcontextfactory = dbcontextfactory;     }      public dosomething(int id)     {         // instance of db use         imycontext dbcontext = this.dbcontextfactory.createmydbcontext();          // use business layer         var user = dbcontext.set<user>().find(id);     } } 

i using ninject factory extension.

is possible make imycontextfactory return same instance of imycontext every time?

background

originally injecting imydbcontext straight service without factory , had inrequestscope() when initialized asp.net mvc website.

but making use of service in windows service , don't want dbcontext become bloated because of frequent looping. didn't want service newed every request either, that's why thought factory within service trick.

i need best of inrequestscope() , new instance every time depending on configuration. have separate config asp.net , windows service - it's how singleton each time factory.

i'm not proficient ninject, according page https://github.com/ninject/ninject.extensions.factory/wiki/factory-interface seems instance returned factory retrieved iresolutionroot.

my take have register imycontext concrete type singleton lifetime type.

(but seems it's not of idea not destroy context, according erik fukenbusch's comment)


Comments