Can I unregister all interface implementations in Autofac?
My scenario:
I am registering two modules, one DefaultModule and later a SpecificModule, if some conditions are met.
builder.RegisterModule(new DefaultModule());
if (someCondition)
{
builder.RegisterModule(new SpecificModule());
}
Two modules register several named instances of the interface, let me 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 the SpecificModule:
builder.RegisterType<SpecificSomething1>().Named<ISomething>("SpecificSomething1").SingleInstance();
builder.RegisterType<SpecificSomething2>().Named<ISomething>("SpecificSomething2").SingleInstance();
When I register a SpecificModule, I want to undo all previous ISomething registrations, since they are being entered as a collection into another constructor.
public SomeClass(IEnumerable<ISomething> somethingCollection)
{
_somethingCollection = somethingCollection;
}
Is it possible? Or is it better to do it differently?
source
share