2009-11-21 5 views
1

Existe-t-il un moyen d'obtenir une référence à NHibernate Configuration au moment de l'exécution? J'en ai besoin pour SchemaExport(). Mise à jour: J'utilise StructureMap avec FluentNHibernate pour le configurer, mais je veux juste savoir si je peux l'obtenir à partir de SessionFactory ou d'un autre objet, après l'initialisation de SessionFactory, sans avoir à réécrire l'installation dans ioc pour tenir sur pour faire référence à la configuration.Référence de configuration de NHibernate à l'exécution

+0

Bien sûr. Comment créez-vous votre configuration initiale? fluent-nhibernate? ActiveRecord? manuellement? –

+0

Pourriez-vous élaborer un peu s'il vous plaît? Je pense que j'ai traité un problème similaire, mais je ne veux pas vous pointer dans la mauvaise direction. – jamesaharvey

Répondre

0

Ok, voici comment je l'ai fait.

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

Maintenant, pour obtenir ce que je viens de faire:

var cfg = ObjectFactory.GetNamedInstance<Configuration>("SharpMod_Configuration"); 
Questions connexes