【问题标题】:When using Dependency Injection with StructureMap how do I chooose among multiple constructors?将依赖注入与 StructureMap 一起使用时,如何在多个构造函数中进行选择?
【发布时间】:2010-04-25 17:45:25
【问题描述】:

我正在尝试获取结构图来为我的一些集成测试构建 Fluent Nhibernate 的 SessionSource 对象。唯一的问题是Fluent对ISessionSourceSessionSource)的具体实现有3个构造函数:

    public SessionSource(PersistenceModel model) 
    {
        Initialize(new Configuration().Configure(), model);
    }

    public SessionSource(IDictionary<string, string> properties, PersistenceModel model)
    {
        Initialize(new Configuration().AddProperties(properties), model);
    }

    public SessionSource(FluentConfiguration config)
    {
        configuration = config.Configuration;

        sessionFactory = config.BuildSessionFactory();
        dialect = Dialect.GetDialect(configuration.Properties);
    }

我已经尝试配置我的ObjectFactory 为第一个构造函数提供一个参数,但它似乎想尝试第二个构造函数。

如何配置我的ObjectFactory,以便我可以选择第一个构造函数,或者如果我决定使用它,甚至可以选择另一个构造函数?

【问题讨论】:

    标签: fluent-nhibernate dependency-injection structuremap ioc-container


    【解决方案1】:

    在您的注册表中,您可以这样做:

    SelectConstructor<SessionSource>(()=> new SessionSource((FluentConfiguration)null));
    For<SessionSource>().Use<SessionSource>();
    

    【讨论】:

      猜你喜欢
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      相关资源
      最近更新 更多