NHibernate - 覆盖配置

在本章中,我们将介绍如何覆盖NHibernate配置.您需要记住几件事.

  • 首先,NHibernate中的配置是附加的.

  • 因此,您不必只使用单个xml文件,或者您不必使用基于代码的配置或流畅的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(),我们可以更改其中哪一个覆盖另一个.