2014-02-07 50 views
2

任何人都知道如何動態配置連接字符串,同時將其他參數保存在cfg.xml文件中?可以使用cfg.xml和編程來配置單個會話工廠嗎?

遵循什麼應該被包含在cfg.xml文件:

  • 方言
  • 連接驅動
  • 查詢換人

,需要進行編程配置的內容:

  • 連接字符串(使用每用戶和每個實例)

這意味着連接字符串動態生成的,而其它參數應在cfg.xml文件靜態的(直到他們改變在XML中手動)。

我想知道這是否可以做到這一點?

NHibernate using single configuration file to connect to multiple dbs

序列

  • 後啓動的應用
  • 應用程序提示用戶輸入兩個他的憑據和數據庫實例
  • 應用驗證對認證數據庫和eith呃根據數據庫響應授予或拒絕訪問。

回答

2

我們可以將兩者結合起來。有一個例子,首先閱讀配置,然後根據需要使用盡可能多的設置擴展它:

ISessionFactory CreateSessionFactory(string connectionString) // from outer source 
{ 
    // the Configuration, i.e.: native, NHibernate one 
    var configuration = new NHibernate.Cfg.Configuration(); 

    var configFile = ... // a path to configuration ; 

    // read that configuration file  
    var document = XDocument.Load(configFile); 

    // pass it to the Configuration 
    using (var reader = document.CreateReader()) 
    { 
     configuration.Configure(reader); 
    } 

    // so, the config file is applied, 
    // no its our dynamic turn 
    configuration.SetProperty("connection.connection_string", connectionString); 
    configuration.SetProperty(....); 

    // and now, we can ask for configuration to produce the factory 
    var factory = configuration.BuildSessionFactory(); 

    return factory; 
} 
+0

+1我終於完成了非常相似的工作。感謝您的回答,我希望這會幫助其他人。 –

+1

太棒了,很高興看到這一點。 NHibernate是令人驚歎的工具;) –

相關問題