我有一个 Fluent NHibernate 项目,我需要在 NHibernate 配置上设置一些任意属性。我的第一次尝试是这样的,我从那里开始Fluently.Configure()
,做一些流利的配置和调用BuildConfiguration()
,然后在该配置上设置一些属性,最后ISessionFactory
从中构建一个。
public static class NHibernateConfigurator
{
private static readonly Configuration _configuration;
private static readonly ISessionFactory _sessionFactory;
static NHibernateConfigurator()
{
_configuration = Fluently.Configure()
.Database(SQLiteConfiguration
.Standard
.InMemory()
.Provider<TestConnectionProvider>()
.FormatSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<AcademicTermMapping>())
.BuildConfiguration();
_configuration
.SetProperty(Environment.CollectionTypeFactoryClass, typeof(Net4CollectionTypeFactory).AssemblyQualifiedName)
.SetProperty(Environment.CurrentSessionContextClass, "thread_static");
var props = _configuration.Properties;
if (props.ContainsKey(Environment.ConnectionStringName))
props.Remove(Environment.ConnectionStringName);
_sessionFactory = _configuration.BuildSessionFactory();
}
虽然我最终得到了一个工作会话工厂,.SetProperty
但中间的调用似乎被忽略了。(特别是,我知道Environment.CollectionTypeFactoryClass
被忽略了,因为我得到了异常。我假设它的其余部分也被忽略了。)
下一次尝试用于ExposeConfiguration()
设置属性:
static NHibernateConfigurator()
{
_configuration = Fluently.Configure()
.Database(SQLiteConfiguration
.Standard
.InMemory()
.Provider<TestConnectionProvider>()
.FormatSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<AcademicTermMapping>())
.ExposeConfiguration(ConfigModifier)
.BuildConfiguration();
_sessionFactory = _configuration.BuildSessionFactory();
}
private static void ConfigModifier(Configuration config)
{
config.SetProperty(Environment.CollectionTypeFactoryClass, typeof(Net4CollectionTypeFactory).AssemblyQualifiedName);
config.SetProperty(Environment.CurrentSessionContextClass, "thread_static");
var props = config.Properties;
if (props.ContainsKey(Environment.ConnectionStringName))
props.Remove(Environment.ConnectionStringName);
}
但这似乎也不起作用。幸运的是,其中一个属性有对应的 fluent 方法,所以我可以这样做:
_configuration = Fluently.Configure()
.CollectionTypeFactory<Net4CollectionTypeFactory>()
// ... etc...
但其余部分没有流利的等价物。我觉得我一定做错了。
更新
现在越来越奇怪了。按照@Firo 的建议,我检查了BuildConfiguration()
. 在配置上手动设置属性.SetProperty
,或者使用流利.CollectionTypeFactory<Net4CollectionTypeFactory>()
的在配置上设置相同的属性。但它们最终以不同的顺序结束。所以我尝试在新配置上手动设置属性,然后从那里继续:
static NHibernateConfigurator()
{
var baseConfig = new Configuration();
baseConfig.SetProperty(Environment.CollectionTypeFactoryClass, typeof(Net4CollectionTypeFactory).AssemblyQualifiedName);
_configuration = Fluently.Configure(baseConfig)
.Database(SQLiteConfiguration
.Standard
.InMemory()
.Provider<TestConnectionProvider>()
.FormatSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<AcademicTermMapping>())
.ExposeConfiguration(ConfigModifier)
.BuildConfiguration();
_sessionFactory = _configuration.BuildSessionFactory();
}
private static void ConfigModifier(Configuration config)
{
config.SetProperty(Environment.CurrentSessionContextClass, "thread_static");
var props = config.Properties;
if (props.ContainsKey(Environment.ConnectionStringName))
props.Remove(Environment.ConnectionStringName);
}
那行得通。不同的方法产生所有相同的属性,只是顺序不同。以上产生这些_configuration.Properties
:
[0] = {[use_reflection_optimizer, True]}
[1] = {[collectiontype.factory_class, DataAccess.NHibernate.Net4CollectionTypeFactory, DataAccess.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]}
[2] = {[connection.provider, DataAccess.NHibernate.Tests.TestConnectionProvider, DataAccess.NHibernate.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]}
[3] = {[connection.driver_class, NHibernate.Driver.SQLite20Driver, NHibernate, Version=3.3.1.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4]}
[4] = {[dialect, NHibernate.Dialect.SQLiteDialect, NHibernate, Version=3.3.1.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4]}
[5] = {[hibernate.dialect, NHibernate.Dialect.SQLiteDialect, NHibernate, Version=3.3.1.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4]}
[6] = {[query.substitutions, true=1;false=0]}
[7] = {[connection.release_mode, on_close]}
[8] = {[format_sql, true]}
[9] = {[connection.connection_string, Data Source=:memory:;Version=3;New=True;]}
[10] = {[current_session_context_class, thread_static]}
其他方法collectiontype.factory_class
按顺序移动,但顺序无关紧要。