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
2 answers
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