2009-11-21 149 views
0

如何重写以下内容,以便以后可以执行ObjectFactory.GetNamedInstance(“MyNHConfiguration”)。 “配置”位于ExposeConfiguration下的变量“cfg”中。lambda需要StructureMap帮助

  ForRequestedType<ISessionFactory>() 
      .CacheBy(InstanceScope.Singleton) 
      .AddInstances(x => x.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=xxcxcca;Password=password;Database=cccvddd;") 
            .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 
                }) 

           .BuildSessionFactory()) 
           .WithName("MySessionFactory")); 

回答

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