I have abstract generic class
public abstract class AbstractLogic<T> {}
And two implementations
DefaultLogic : AbstractLogic<ClassA>{}
SpecificLogic : AbstractLogic<ClassB>{}
As we see, if I want to create instance of oone of them I can easily do it without specifing open generic class.
DefaultLogic logic= new DefaultLogic();
Now I need to create instance of each of them with the help of DI. So, I registered types like that
var logicList= new[]
{
Assembly.GetAssembly(typeof(DefaultLogic)),
Assembly.GetAssembly(typeof(SpecificLogic))
};
builder.RegisterAssemblyTypes(logicList).AsClosedTypesOf(typeof(AbstractLogic<>))
.Where(t => t.Name.StartsWith(Settings.GetCurrentMode().ToString()));
Settings.GetCurrentMode() - returns me a name of instance that I need (Default of Specific).
No I want to Inject service into controller to make it able to load needed logic service.
public class ListController : Controller
{
private AbstractLogic<???> _logic;
public ListController(AbstractLogic<???> logic)
{
_logic = logic;
}
}
Compiler asks me to define model instead of ???. But, I don't need it, since Implementation ob abstract class already chose an open generic type over inheritance. Is there other way how can I resolve needed logic with autofac?