【发布时间】:2011-06-20 06:24:24
【问题描述】:
是否可以轻松配置 autofac,使其仅使用非过时的构造函数进行解析?
例如,对于具有非 DI 代码的辅助构造函数的类,
public class Example {
public Example(MyService service) {
// ...
}
[Obsolete]
public Example() {
service = GetFromServiceLocator<MyService>();
// ...
}
}
// ....
var builder = new ContainerBuilder();
builder.RegisterType<Example>();
// no MyService defined.
var container = builder.Build();
// this should throw an exception
var example = container.Resolve<Example>();
如果我们没有注册 MyService,要求 autofac 解析 Example 应该会失败。
【问题讨论】: