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(" Fetch the complete list again "); foreach (var student in students) { Console.WriteLine("{0} {1} {2} {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() 的调用顺序,我们可以改变其中哪个配置覆盖另一个配置。