我的会话会自动关闭吗?

时间:2011-01-23 00:33:08

标签: asp.net-mvc nhibernate fluent-nhibernate asp.net-mvc-3 ninject

修改

Orignal Title:我的交易在到达我的回购时关闭。我做错了什么?

我得到了我的origanl问题的答案(我忘了打开交易大声笑)。现在我想知道我的代码是自动关闭会话还是我必须以某种方式告诉它这样做。


您好

我使用的是mvc 3.0,nhibernate,流利的nhibernate和ninject 2.0

Global.asax中

// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            // Hook our DI stuff when application starts
            SetupDependencyInjection();


            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }


        public void SetupDependencyInjection()
        {         
            // Tell ASP.NET MVC 3 to use our Ninject DI Container
            DependencyResolver.SetResolver(new NinjectDependencyResolver(CreateKernel()));
        }

        protected IKernel CreateKernel()
        {
            var modules = new INinjectModule[]
                              {
                                 new NhibernateModule(),
                                 new ServiceModule(),
                                 new RepoModule()
                              };

            return new StandardKernel(modules);
        }

    }

会话工厂

public class NhibernateSessionFactory
    {
        public ISessionFactory GetSessionFactory()
        {
            ISessionFactory fluentConfiguration = Fluently.Configure()
                                                  .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("test")))
                                                  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyMaps>())
                                                  .BuildSessionFactory();

            return fluentConfiguration;
        }
    }

会话工厂提供商

 public class NhibernateSessionFactoryProvider : Provider<ISessionFactory>
    {   
        protected override ISessionFactory CreateInstance(IContext context)
        {
            var sessionFactory = new NhibernateSessionFactory();
            return sessionFactory.GetSessionFactory();
        }
    }

Nhibernate模块

 public class NhibernateModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ISessionFactory>().ToProvider<NhibernateSessionFactoryProvider>().InSingletonScope();
            Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
        }
    }

服务模块

  public class ServiceModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ITest>().To<Test>();
        }
    }

回购模块

 public class RepoModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IStudentRepo>().To<StudentRepo>();
        }
    }

的HomeController

 private readonly ITest test;
        public HomeController(ITest test)
        {
            this.test = test;
        }

        //
        // GET: /Home/
        public ActionResult Index()
        {
           return View();
        }

测试(我的服务层文件)

  public class Test : ITest
    {
        private readonly IStudentRepo studentRepo;

        public Test(IStudentRepo studentRepo)
        {
            this.studentRepo = studentRepo;
        }

    }

回购

  public class StudentRepo : IStudentRepo
    {
        private readonly ISession session;

        public StudentRepo(ISession session)
        {
            this.session = session;
        }
    }

当我在进入我的回购的会话中查看调试器时。它说会话是打开并连接的,但是(session.Transaction).IsActive = false

1 个答案:

答案 0 :(得分:2)

您目前正在设置使用隐式交易,我认为这些交易不会通过session.Transaction公开。当然,Use of implicit transactions is discouraged