需要StructureMap帮助

时间:2009-11-21 01:20:27

标签: nhibernate fluent-nhibernate structuremap

如何重写以下内容,以便以后可以进行ObjectFactory.GetNamedInstance(“MyNHConfiguration”)。 “配置”位于ExposeConfiguration lambda

下的变量“cfg”中
           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"));

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