NHibernate Configuration reference at runtime
Is there a way to g开发者_运维技巧et a reference to NHibernate Configuration at the runtime? I need it for SchemaExport().
Update: I am using StructureMap with FluentNHibernate to set it up, but I just want to know if I can get it from SessionFactory or some other object, after SessionFactory has been initialized, without having to rewrite setup in ioc to hold on to reference to Configuration.
Ok, here is how I did it.
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"));
Now to get it I just do:
var cfg = ObjectFactory.GetNamedInstance<Configuration>("SharpMod_Configuration");
精彩评论