开发者

NHibernate Session Management w/ Ninject

Is this really all that I have to do to achieve session per request with Ninject?

public class WebModule : NinjectModule
{
    public override void Load()
    {            
        Bind<ISession>().ToMethod(x => MvcApplication.SessionFactory.OpenSession()).InRequestScope();            
    }
}

Global.asax:

public class MvcApplication : NinjectHttpApplication
{
    public static ISessionFactory SessionFactory = CreateSessionFactory();

    public static void RegisterRoutes(RouteCollection routes) { /* Routing Stuff */ }

    protecte开发者_C百科d override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
    }

    protected override void OnApplicationStopped()
    {
        SessionFactory.Dispose();
    }

    protected static ISessionFactory CreateSessionFactory() { /* session factory stuff */ }

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new WebModule());
    }
}

Is there anything wrong with this? It just seems too easy.


After reading this post I decided not to do it this way. Unless someone speaks up, I'll just do it the old fashioned way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜