问题描述
我需要通过 StructureMap 实例化以下类型:
I have the following type which I need to be instantiated by 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个参数.我已经看到了如何为 StructureMap 提供 one 参数的示例(例如 在使用 StructureMap 时传递构造函数参数) 但我不确定我需要做什么来传递 3.
The problem is that this constructor takes 3 parameters. I've seen examples how to provide StructureMap with one parameter (e.g. Passing constructor arguments when using StructureMap) but I'm not sure what I need to do to pass 3 in.
是否只是以下情况:
For<IAWebService>().Use<AWebService>()
.Ctor<string>("applicationId").Is(GetIDFromConfig())
.Ctor<string>("username").Is(GetUsernameFromConfig())
.Ctor<string>("password").Is(GetPasswordFromConfig());
还是我必须以不同的方式对其进行配置?
or do I have to configure it differently?
推荐答案
您是否尝试过问题中的代码,它看起来是正确的.
Have you tried the code in your question, it looks correct.
使用结构映射注册您的类型并设置字符串构造函数参数
Register your type with structuremap and set the string constructor parameters
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");
});
然后您可以使用设置的参数获取您的服务.
Then you can get your service with the parameters set.
var service = container.GetInstance<IAWebService>();
这篇关于如何配置 StructureMap 以采用多个构造函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!