Is it possible to unregister all implementations of an interface in Autofac?
My scenario:
I register two modules, one DefaultModule and later a SpecificModule if some conditions are fulfilled.
builder.RegisterModule(new DefaultModule());
if (someCondition)
{
builder.RegisterModule(new SpecificModule());
}
The two modules register multiple named instances of an interface, let's call it ISomething.
Inside the Load function in DefaultModule:
builder.RegisterType<DefaultSomething1>().Named<ISomething>("DefaultSomething1").SingleInstance();
builder.RegisterType<DefaultSomething2>().Named<ISomething>("DefaultSomething2").SingleInstance();
Inside the Load function in SpecificModule:
builder.RegisterType<SpecificSomething1>().Named<ISomething>("SpecificSomething1").SingleInstance();
builder.RegisterType<SpecificSomething2>().Named<ISomething>("SpecificSomething2").SingleInstance();
When I register the SpecificModule I want to unregister all previous registrations of ISomething since they are injected as a collection into another constructor.
public SomeClass(IEnumerable<ISomething> somethingCollection)
{
_somethingCollection = somethingCollection;
}
Is this possible? Or is it better to do it in another way?