开发者

StructureMap help needed

How can I rewrite following so that I can do ObjectFactory.GetNamedInstance("MyNHConfiguration") at later time. "Configuration" is in the variable "cfg" under ExposeConfiguration lambda

           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()
     开发者_JAVA技巧                               .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"));


Here is how I did it. Don't know if it is the best way but it works

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜