【问题标题】:StructureMap help needed需要结构图帮助
【发布时间】:2009-11-21 01:20:26
【问题描述】:

如何重写以下内容,以便以后可以执行 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"));

【问题讨论】:

    标签: nhibernate fluent-nhibernate structuremap


    【解决方案1】:

    这就是我的做法。不知道这是否是最好的方法,但它有效

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      相关资源
      最近更新 更多