NHibernate - 配置



本章我們將介紹 NHibernate 的配置。我們可以透過不同的方式配置 NHibernate,主要分為兩類:

  • 基於XML的配置
  • 基於程式碼的配置

基於程式碼的配置

基於程式碼的配置是 NHibernate 內建的功能。它在 NHibernate 3 左右引入,我們一直以來都在使用基於程式碼的配置。

String Data Source = asia13797\\sqlexpress;
String Initial Catalog = NHibernateDemoDB;
String Integrated Security = True;
String Connect Timeout = 15;
String Encrypt = False;
String TrustServerCertificate = False;
String ApplicationIntent = ReadWrite;
String MultiSubnetFailover = False;

cfg.DataBaseIntegration(x = > { x.ConnectionString = "Data Source + 
   Initial Catalog + Integrated Security + Connect Timeout + Encrypt +
   TrustServerCertificate + ApplicationIntent + MultiSubnetFailover"; 
   
   x.Driver<SqlClientDriver>(); 
   x.Dialect<MsSql2008Dialect>(); 
   x.LogSqlInConsole = true; 
}); 

cfg.AddAssembly(Assembly.GetExecutingAssembly());

所有配置都在 C# 程式碼中指定。在這裡您可以看到我們建立了一個新的配置物件,然後我們使用在 NHibernate 3.1 中引入的**流暢配置 (loquacious configuration)** 來配置資料庫:我們使用哪個連線字串,連線到哪個資料庫以及要使用的方言。我們還直接在這裡添加了我們的對映程式集。

基於XML的配置

如果您使用基於 XML 的配置,可以使用一個**hibernate.cfg.xml**檔案,它只是一個使用 NHibernate 架構的獨立 XML 檔案,或者您可以將 NHibernate 特定的配置嵌入到您的應用程式或**web.cfg**中。hibernate.cfg.xml 是預設名稱,但我們也可以為該 XML 檔案使用任意名稱。

讓我們透過向 NHibernateDemoApp 專案新增一個名為 hibernate.cfg.xml 的新 XML 檔案來了解基於 XML 的配置。

將以下資訊輸入 hibernate.cfg.xml 檔案。

<?xml version = "1.0" encoding = "utf-8" ?> 
<hibernate-configuration xmlns = "urn:nhibernate-configuration-2.2"> 
   <session-factory> 
   
      <property name = "connection.connection_string">
         Data Source = asia13797\\sqlexpress;
         Initial Catalog = NHibernateDemoDB;
         Integrated Security = True;
         Connect Timeout = 15;
         Encrypt = False;
         TrustServerCertificate = False;
         ApplicationIntent = ReadWrite;
         MultiSubnetFailover = False;
      </property> 
      
      <property name = "connection.driver_class">
         NHibernate.Driver.SqlClientDriver
      </property> 
		
      <property name = "dialect">
         NHibernate.Dialect.MsSql2008Dialect
      </property> 
		
      <mapping assembly = "NHibernateDemoApp"/>
		
   </session-factory> 
	
</hibernate-configuration>

正如您在上面的 XML 檔案中看到的,我們指定了與 C# 中提到的相同的配置。

現在,讓我們註釋掉 Program.cs 檔案中的配置,只調用**Configure()**方法,它將載入**hibernate.cfg.xml**檔案,如下所示。

using HibernatingRhinos.Profiler.Appender.NHibernate; 
using NHibernate.Cfg; 
using NHibernate.Dialect; 
using NHibernate.Driver; 

using System; 
using System.Linq; 
using System.Reflection; 

namespace NHibernateDemoApp { 

   class Program { 
      
      static void Main(string[] args) { 
		
         NHibernateProfiler.Initialize(); 
         var cfg = new Configuration(); 
         
         //cfg.DataBaseIntegration(x =>
         
         //{ 
            // x.ConnectionString = "Data Source = asia13797;\\sqlexpress
            Initial Catalog = NHibernateDemoDB;
            Integrated Security = True;
            Connect Timeout = 15;
            Encrypt =False;
            TrustServerCertificate = False;
            ApplicationIntent = ReadWrite;
            MultiSubnetFailover = False"; 
            
            // x.Driver<SqlClientDriver>(); 
            // x.Dialect<MsSql2008Dialect>(); 
            // x.LogSqlInConsole = true; 
         //}); 
         
         //cfg.AddAssembly(Assembly.GetExecutingAssembly());
			
         cfg.Configure();
         var sefact = cfg.BuildSessionFactory();
			
         using (var session = sefact.OpenSession()) { 
            
            using (var tx = session.BeginTransaction()) { 
               var students = session.CreateCriteria<Student>().List<Student>(); 
               Console.WriteLine("\nFetch the complete list again\n"); 
               
               foreach (var student in students) { 
                  Console.WriteLine("{0} \t{1} \t{2} \t{3}", student.ID,
                     student.FirstName, student.LastName, student.AcademicStanding); 
               } 
					
               tx.Commit(); 
            } 
				
            Console.ReadLine(); 
         } 
      } 
   } 
}

讓我們再次執行您的應用程式,您將看到相同的輸出。

Fetch the complete list again

1 Allan Bommer Excellent
2 Jerry Lewis Good
廣告
© . All rights reserved.