我有一个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);
}
但这似乎也不起作用。幸运的是,其中一个属性有一个相应的流畅方法,所以我可以这样做:
_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
,但顺序无关紧要。