多个构造函数与 Structuremap 改变范围?
为了说明问题,这是我的设置的简化版本。 我有一个像这样的工厂:
public interface IFactory{ }
public class Factory : IFactory
{
public Factory()
{
Console.WriteLine("parameterless");
}
//public Factory(int i)
//{
// Console.WriteLine("with parameter : {0}", i);
//}
}
测试它的程序是一个consoleApp。足以证明我的观点。
static void Main(string[] args)
{
Init();
var factory1 = ObjectFactory.GetInstance<IFactory>();
var factory2 = ObjectFactory.GetInstance<IFactory>();
var factory3 = ObjectFactory.GetInstance<IFactory>();
Console.Read();
}
我在 Init 静态方法中设置 structureMap 。
public static void Init()
{
ObjectFactory.Initialize(x =>
{
//insert Initialization code here.
});
}
如果我只有一个构造函数并像这样设置 StructureMap :
x.For<IFactory>().Use<Factory>();
效果很好,并且输出显示
无参数
无参数
无参数
每次调用都会构建一个新实例。
现在,如果我取消注释第二个构造函数,但我想使用无参数构造函数,并具有相同的默认生活方式。我该怎么办?
我尝试过:
x.SelectConstructor<IFactory>(() => new Factory());
x.For<IFactory>().Use<Factory>();
它只是不起作用:缺少 InstanceKey 的请求实例属性“i”
如果我这样做:
x.For<IFactory>().Use(new Factory());
它可以工作,但输出只是一个“无参数”,这意味着它不会为每次调用构建一个新实例。它使用我传入的这个特定实例。
我发现的唯一方法是在无参数构造函数顶部添加 [DefaultConstructor] 并使用标准 x.For().Use(); 但我不想添加此属性并将配置分布到我的模型中。
帮助?
To illustrate the problem, here is a simplified version of my setup.
I have a factory like this one :
public interface IFactory{ }
public class Factory : IFactory
{
public Factory()
{
Console.WriteLine("parameterless");
}
//public Factory(int i)
//{
// Console.WriteLine("with parameter : {0}", i);
//}
}
the program to test this is a consoleApp. Enough to prove my point.
static void Main(string[] args)
{
Init();
var factory1 = ObjectFactory.GetInstance<IFactory>();
var factory2 = ObjectFactory.GetInstance<IFactory>();
var factory3 = ObjectFactory.GetInstance<IFactory>();
Console.Read();
}
I setup strucutreMap in my Init static method.
public static void Init()
{
ObjectFactory.Initialize(x =>
{
//insert Initialization code here.
});
}
If I have only one constructor and setup StructureMap like this :
x.For<IFactory>().Use<Factory>();
that works perfect, and the output shows
parameterless
parameterless
parameterless
Each call builds a new instance.
Now if I uncomment the second constructor, but I want to use the parameterless one, and with the same default lifestyle. How would I do?
I tried that :
x.SelectConstructor<IFactory>(() => new Factory());
x.For<IFactory>().Use<Factory>();
It just doesn't work : Missing requested Instance property "i" for InstanceKey
If I do like this :
x.For<IFactory>().Use(new Factory());
It works, but the output is just one "parameterless" meaning it's not building a new instance for each call. It's using this specific instance I pass in.
The only way I found is to add the [DefaultConstructor] on top of my parameterless constructor and use the standard x.For().Use();
But I don't want to add this attribute and spread the configuration accross my model.
Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SelectConstructor 采用具体类型,而不是接口。当然,接口没有任何构造函数。
但没有任何问题,它只是被忽略了......所以我无法发现该错误。
应该是
SelectConstructor takes the concrete type, not the interface. Of course, an interface doesnt have any constructor.
But nothing breaks, it's just ignored.... so I couldnt spot that error.
should be