2010-04-07 68 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之外的兩個其他選項。

相關問題