NHibernate – 覆盖配置
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()的顺序,我们可以更改哪个他们覆盖另一个。