问题描述
我们正在努力与理解这两种方式来配置StructureMap之间的差异。我们的理解是,他们应该是相同的,但我们得到以下两行初始化内部之间不同的结果:
We're struggling with understanding the difference between these two ways to configure StructureMap. Our understanding is that they should be identical but we get different results between these two lines inside of Initialize:
ObjectFactory.Initialize(x =>
{
x.For<IBusinessRelationsContext>().Use<BusinessRelationsContext>().Ctor<string>().Is(ConfigurationManager.ConnectionStrings["BusinessRelationsContext"].ConnectionString);
x.For<IBusinessRelationsContext>().Use(_ => new BusinessRelationsContext(ConfigurationManager.ConnectionStrings["BusinessRelationsContext"].ConnectionString));
});
(我们只用1两个在同一时间 - 不能同时使用,很明显)
(we only use 1 of the two at a time - not both, obviously)
我们的这个对象在不同的构造特征(这是EF4的东西,如果你在乎):
Our various constructor signatures on this object (it's EF4 stuff if you care):
public BusinessRelationsContext();
public BusinessRelationsContext(string connectionString);
public BusinessRelationsContext(EntityConnection connection);
我们用它来调用这个的code是:
The code we use to invoke this is:
ObjectFactory.TryGetInstance<IBusinessRelationsContext>();
这是我们看到行为不同的是,包括该行男星&LT;字符串&GT;
失败,因为StructureMap失败与PluginFamily System.Data这定义了一个202没有默认实例.Common.DbConnection(我们不知道为什么它认为它需要这个)。不过,如果我评论说,线路输出,并使用另一个,它完美的作品,因为我们所期望的。考虑到另外一个作品,我怀疑我的理解,它不应该需要配置的的DbConnection是正确的。
The difference in behavior that we see is that the line that includes Ctor<string>
fails because StructureMap fails with a 202 "No Default Instance defined for PluginFamily System.Data.Common.DbConnection" (we have no idea why it thinks it needs this). However, if I comment that line out and use the other one, it works perfectly as we would expect. Given that the other one works, I suspect that my understanding that it shouldn't need config for DbConnection is correct.
因此,而不是跟踪为什么它需要的DbConnection我宁愿追查回答我的问题:什么是这两者之间的区别
So rather than tracking down WHY it needs the DbConnection I would rather track down the answer to my question: What's the difference between these two?
推荐答案
我觉得StructureMap是选择最复杂的构造函数来尝试创建自己的DataContext。您已经定义了那里的构造函数调用什么是关于如何使用不太复杂的构造来定义类的定义。
I think StructureMap is selecting the most complex constructor to try to create your datacontext. What you have defined up there with the Ctor call is a definition on how to define that class using a less complex constructor.
所以你的定义是不是不正确,它只是StructureMap不喊你认为它应该是调用构造函数。
So your definition isn't incorrect, it's just that StructureMap isn't calling the constructor you think it's supposed to be calling.
注意:我通常使用你的第二个电话,因为我知道什么是构造函数被调用,即使你要添加新的测试或其他目的
x.For<IBusinessRelationsContext>().Use(_ => new BusinessRelationsContext(ConfigurationManager.ConnectionStrings["BusinessRelationsContext"].ConnectionString));
这篇关于什么是这两个StructureMap的configs之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!