2009-11-21 53 views
1

有沒有辦法在運行時獲得對NHibernate配置的引用?我需要它的SchemaExport()。我使用StructureMap和FluentNHibernate來設置它,但我只想知道是否可以在SessionFactory初始化後從SessionFactory或其他對象中獲取它,而不必重寫ioc中的安裝程序來保存以引用配置。NHibernate運行時配置參考

+0

肯定。你如何創建你的初始配置?流利,NHibernate的? ActiveRecord的?手動? – 2009-11-21 01:30:38

+0

請您詳細說明一下嗎?我想我已經處理過類似的問題,但不想指出錯誤的方向。 – jamesaharvey 2009-11-21 02:25:50

回答

0

好的,這是我是如何做到的。

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");