运行时的NHibernate配置参考

时间:2009-11-21 01:06:39

标签: nhibernate

有没有办法在运行时获得对NHibernate配置的引用?我需要它用于SchemaExport()。

更新:我正在使用带有FluentNHibernate的StructureMap进行设置,但我只是想知道在SessionFactory初始化之后是否可以从SessionFactory或其他对象获取它,而无需在ioc中重写设置以保持引用配置。

1 个答案:

答案 0 :(得分: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");