是否可以在Autofac中注销接口的所有实现?
我的情况:
我注册了两个模块,如果满足某些条件,则注册一个DefaultModule,然后注册一个SpecificModule。
builder.RegisterModule(new DefaultModule());
if (someCondition)
{
builder.RegisterModule(new SpecificModule());
}
Run Code Online (Sandbox Code Playgroud)
这两个模块注册了接口的多个命名实例,我们称其为ISomething。
在DefaultModule中的Load函数内部:
builder.RegisterType<DefaultSomething1>().Named<ISomething>("DefaultSomething1").SingleInstance();
builder.RegisterType<DefaultSomething2>().Named<ISomething>("DefaultSomething2").SingleInstance();
Run Code Online (Sandbox Code Playgroud)
在SpecificModule中的Load函数内部:
builder.RegisterType<SpecificSomething1>().Named<ISomething>("SpecificSomething1").SingleInstance();
builder.RegisterType<SpecificSomething2>().Named<ISomething>("SpecificSomething2").SingleInstance();
Run Code Online (Sandbox Code Playgroud)
当我注册SpecificModule时,我想取消注册ISomething的所有先前注册,因为它们作为一个集合注入到另一个构造函数中。
public SomeClass(IEnumerable<ISomething> somethingCollection)
{
_somethingCollection = somethingCollection;
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?还是以另一种方式更好?
| 归档时间: |
|
| 查看次数: |
1436 次 |
| 最近记录: |