我有一个通用界面:
public interface IRepository<T> { ... }
我有这样的实现:
public class Repository<T> {
public Repository<T>() { ... }
}
StructureMap(v 2.6.3)配置如下:
For(typeof(IRepository<>)).Use(typeof(Repository<>));
当我尝试从StructureMap中提取IRepository<Something>
时,我按预期得到Repository<Something>
。的万岁!
现在我添加了第二个构造函数,实现如下:
public class Repository<T> {
public Repository<T>() { ... }
public Repository<T>(string database) { ... }
}
当我尝试获得IRepository<Something>
时,我得到一个异常,因为它默认尝试使用带有参数的新构造函数。的啵!
如何更改StructureMap配置以便它知道使用无参数构造函数?
答案 0 :(得分:5)
您可以通过标记希望StructureMap与[DefaultConstructor]属性一起使用的构造函数来实现此目的。正如您在StructureMap documentation上看到的那样,默认情况下它是贪婪的。