1
有没有办法在运行时获得对NHibernate配置的引用?我需要它的SchemaExport()。我使用StructureMap和FluentNHibernate来设置它,但我只想知道是否可以在SessionFactory初始化后从SessionFactory或其他对象中获取它,而不必重写ioc中的安装程序来保存以引用配置。NHibernate运行时配置参考
有没有办法在运行时获得对NHibernate配置的引用?我需要它的SchemaExport()。我使用StructureMap和FluentNHibernate来设置它,但我只想知道是否可以在SessionFactory初始化后从SessionFactory或其他对象中获取它,而不必重写ioc中的安装程序来保存以引用配置。NHibernate运行时配置参考
好的,这是我是如何做到的。
ForRequestedType<FluentConfiguration>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.Is.ConstructedBy(
()=>Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.AdoNetBatchSize(10)
.Driver("NHibernate.Driver.SqlClientDriver")
.ProxyFactoryFactory("NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")
.UseOuterJoin()
.ConnectionString(@"Server=.\SQLEXPRESS;User Id=epitka;Password=password;Database=dnn49;")
.ShowSql()
.CurrentSessionContext("thread_static")) // CHANGE THIS FOR WEB
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MetaProject>())
.ExposeConfiguration(
cfg =>{
cfg.SetProperty(
Environment.TransactionStrategy,
typeof (AdoNetTransactionFactory).FullName);
cfg.SetProperty(Environment.GenerateStatistics, "true"); //REMOVE FOR LIVE
})
)
.WithName("SharMod_FluentConfiguration");
ForRequestedType<Configuration>()
.TheDefault.Is.ConstructedBy(
() =>
{
var fc =ObjectFactory.GetInstance<FluentConfiguration>();
return fc.BuildConfiguration();
})
.WithName("SharpMod_Configuration");
//SharpMod_SessionFactory
ForRequestedType<ISessionFactory>()
.CacheBy(InstanceScope.Singleton)
.AddInstances(x => x.ConstructedBy(() =>
ObjectFactory.GetNamedInstance<FluentConfiguration>("SharMod_FluentConfiguration")
.BuildSessionFactory())
.WithName("SharpMod_SessionFactory"));
我们得到它,我只是做:
var cfg = ObjectFactory.GetNamedInstance<Configuration>("SharpMod_Configuration");
肯定。你如何创建你的初始配置?流利,NHibernate的? ActiveRecord的?手动? – 2009-11-21 01:30:38
请您详细说明一下吗?我想我已经处理过类似的问题,但不想指出错误的方向。 – jamesaharvey 2009-11-21 02:25:50