Nei*_*ilD 3 c# structuremap generics inversion-of-control
我有一个通用的界面:
public interface IRepository<T> { ... }
Run Code Online (Sandbox Code Playgroud)
我有这样的实现:
public class Repository<T> {
public Repository<T>() { ... }
}
Run Code Online (Sandbox Code Playgroud)
StructureMap(v 2.6.3)配置如下:
For(typeof(IRepository<>)).Use(typeof(Repository<>));
Run Code Online (Sandbox Code Playgroud)
当我尝试拉出IRepository<Something>
StructureMap时,我得到了一个Repository<Something>
预期的结果.万岁!
现在我添加了第二个构造函数,实现如下:
public class Repository<T> {
public Repository<T>() { ... }
public Repository<T>(string database) { ... }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试获得一个IRepository<Something>
现在,我得到一个例外,因为它默认尝试使用带有参数的新构造函数.嘘!
如何更改我的StructureMap配置,以便它知道使用无参数构造函数?
您可以通过标记希望StructureMap与[DefaultConstructor]属性一起使用的构造函数来实现此目的.正如您在StructureMap文档中看到的那样,默认情况下它是贪婪的.