如何配置StructureMap以获取多个构造函数参数?

时间:2011-08-03 10:10:00

标签: c# structuremap

我有以下类型需要由StructureMap实例化:

public class AWebService : IAWebService
{
    private readonly string _applicationId;
    private readonly string _username;
    private readonly string _password;

    public AWebService(string applicationId, string username, string password)
    {
        _applicationId = applicationId;
        _username = username;
        _password = password;
    }
}

问题是这个构造函数需要3个参数。我已经看过如何使用一个参数(例如Passing constructor arguments when using StructureMap)为StructureMap提供示例,但我不确定我需要做什么来传递3。

只是一个例子:

For<IAWebService>().Use<AWebService>()
  .Ctor<string>("applicationId").Is(GetIDFromConfig())
  .Ctor<string>("username").Is(GetUsernameFromConfig())
  .Ctor<string>("password").Is(GetPasswordFromConfig());

或者我必须以不同方式配置它吗?

2 个答案:

答案 0 :(得分:6)

您是否尝试过问题中的代码,看起来是正确的。

使用structuremap注册您的类型并设置字符串构造函数参数

var container = new Container(x => {
            x.For<IAWebService>().Use<AWebService>()
                .Ctor<string>("applicationId").Is("my app id")
                .Ctor<string>("username").Is("my username")
                .Ctor<string>("password").Is("my passowrd");
        });

然后,您可以使用参数设置获得服务。

var service = container.GetInstance<IAWebService>();

答案 1 :(得分:1)

我会说你注入类型为IConfigurationHelper的引用或者包含这3个属性的引用。这样您就可以使用从IConfigurationHelper

派生的所有不同类型
相关问题