Allow singleton in Bind <T> (). Tomethod

What is the Ninject 3 equivalent of this code:

Bind<ISession>().ToMethod(ctx => ctx.Kernel.Get<INHibernateSessionFactoryBuilder>() .GetSessionFactory() .OpenSession()) .Using<OnePerRequestBehavior>(); 

I know that instead of Using<OnePerRequestBehavior> I can use InRequestScope , but how to replace ctx.Kernel.Get<INHibernateSessionFactoryBuilder> ? ( INHibernateSessionFactoryBuilder is single)

+4
source share
2 answers

Ok, just clarify - since this is in the module, you can still access ctx.Kernel.Get<T> , but you need to add using Ninject; to the module, since Kernel.Get<T> displayed as an extension method.

+3
source

So here is the final code that works for me:

 using Infrastructure.Data; using NHibernate; using Ninject; using Ninject.Modules; using Ninject.Web.Common; namespace Infrastructure.DependencyResolution { public class SessionModule : NinjectModule { public override void Load() { Bind<INHibernateSessionFactoryBuilder>().To<NHibernateSessionFactoryBuilder>().InSingletonScope(); Bind<ISession>().ToMethod(ctx => ctx.Kernel.Get<INHibernateSessionFactoryBuilder>() .GetSessionFactory() .OpenSession()) .InRequestScope(); } } } 

With this module loaded with the Ninject loader, I can use repositories with an NHibernate session without the need for an NH link in a web project ...

+1
source

All Articles