NHibernate - 覆蓋配置



在本章中,我們將介紹如何覆蓋 NHibernate 配置。您只需要記住幾件事。

  • 首先,NHibernate 中的配置是累加的。

  • 因此,您不必只使用單個 XML 檔案,也不必只使用基於程式碼的配置或 Fluent NHibernate。

  • 您可以根據需要配置應用程式的方式混合和匹配所有這些方法。

  • 需要記住的重要一點是,最後的配置獲勝。

在下面的示例中,您可以看到我們建立了配置物件,使用基於程式碼的配置對其進行配置,最後呼叫了cfg.configure()方法,該方法載入hibernate.cfg.xml檔案。

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.Configure();
  • 因此,hibernate.cfg.xml 中的任何內容都會覆蓋基於程式碼的配置設定。

  • 透過反轉這兩個過程,我們可以將預設值放在 hibernate.cfg.xml 中,然後在基於程式碼的配置中進行覆蓋。

  • 沒有任何東西阻止您同時使用基於程式碼的配置和 hibernate.cfg.xml 檔案。

讓我們來看一個簡單的例子,在這個例子中,我們將透過混合使用基於 XML 和基於程式碼的配置來覆蓋配置。

讓我們也使用以下程式碼將連線字串移動到app.config檔案中。

<?xml version = "1.0" encoding = "utf-8" ?> 

<configuration> 
   
   <startup> 
      <supportedRuntime version = "v4.0" sku = ".NETFramework,Version = v4.5" /> 
   </startup> 
   
   <connectionStrings> 
      <add name = "default" connectionString = "Data Source =
         asia13797\\sqlexpress;
         Initial Catalog = NHibernateDemoDB;
         Integrated Security = True;
         Connect Timeout = 15;
         Encrypt = False;
         TrustServerCertificate = False;
         ApplicationIntent = ReadWrite;
         MultiSubnetFailover = False"/> 
   </connectionStrings> 

</configuration>

連線字串位於某個具有預設名稱的app.config檔案中。現在,我們需要在 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">default</property> 
		
      <property name = "connection.driver_class">
         NHibernate.Driver.SqlClientDriver
      </property> 
		
      <property name = "dialect">
         NHibernate.Dialect.MsSql2008Dialect
      </property> 
		
      <mapping assembly = "NHibernateDemoApp"/> 
   </session-factory> 

</hibernate-configuration>

讓我們註釋掉基於程式碼的配置中的連線字串部分、驅動程式和方言部分,因為程式將從 hibernate.cfg.xml 檔案中讀取它,而LogSqlInConsole部分將保留在基於程式碼的配置中。

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();
			
         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.Configure(); 
         cfg.AddAssembly(Assembly.GetExecutingAssembly()); 
         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(); 
         } 
      } 
   } 
}

現在,當您執行應用程式時,您將看到程式已從基於程式碼的配置中讀取日誌,並從 hibernate.cfg.xml 檔案中讀取其他配置。

NHibernate: SELECT this_.ID as ID0_0_, this_.LastName as LastName0_0_,   
   this_.FirstMidName as FirstMid3_0_0_, this_.AcademicStanding as Academic4_0_0_ FROM
   Student this_

Fetch the complete list again
1 Allan Bommer Excellent
2 Jerry Lewis Good

因此,現在我們的一些配置位於hibernate.cfg.xml檔案中,一些配置位於基於程式碼的配置中,並且根據呼叫基於程式碼的配置與configure()的順序,我們可以更改哪個覆蓋另一個。

廣告