0

編輯我的會議會自動關閉嗎?

Orignal標題:我的交易在到達我的回購時關閉。我究竟做錯了什麼?

我得到一個答案,我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

你從哪裏開始交易? – Vadim 2011-01-23 01:01:08

回答