2010-04-07 71 views

回答

4

GetConfiguration方法在你SessionBuilder,而不是你的链接页面显示的

public Configuration GetConfiguration() 
    { 
     var configuration = new Configuration(); 
     configuration.Configure(); 
     return configuration; 
    } 

,简单地做这样的事情:

public Configuration GetConfiguration() 
    { 
     return Fluently.Configure() 
      .Database(/* your database settings */) 
      .Mappings(/* your mappings */) 
      .ExposeConfiguration(/* alter Configuration */) // optional 
      .BuildConfiguration(); 
    } 

至于有关处理环境的进一步询问,你会有两个类继承ISessionBuilder,例如AspSessionBuilderWinAppSessionBuilder,并为当前项目注入适当的一个。您应该使用Jamie Ideposted as an answer概述的策略处理上下文而不是使用HttpContext。你只需简单地要修改此行:

.ExposeConfiguration(x => x.SetProperty("current_session_context_class", "web") 

喜欢的东西"call""thread_static"。针对不同情境会话类型的一个很好的解释见NHibernate的锻造维基此页:

Contextual Sessions @ NHibernate Forge

+0

不错,谢谢...这是否适合除asp.net以外的其他应用程序? - 我打算在windows服务中使用我的存储库也是 – Alex 2010-04-07 22:46:01

+0

是的。你应该没问题。我在我所有的应用程序中使用类似的存储库模式。 – snicker 2010-04-07 22:47:02

+0

谢谢:-)在该链接的例子中,有getExistingOrNewSession - 它检查HttpContext - 在Windows应用程序中使用它时如何工作? – Alex 2010-04-08 08:40:55

1

是的,你可以使用它,但它最好使用NHibernate的内置的情境会话管理,而不是自己处理。看到我的回答this question。除了较少的编码之外,它还提供了除HttpContext,Call和ThreadStatic之外的两个其他选项。

相关问题