jeudi 2 juillet 2020

Autofac register multiple interface hierachy, multiple concrete classes

i don't seem to find an answer for my kind of problem. So, let's say have an interface

public interface IBase {        
void MethodBase();    
}

and a class implementing this, BaseClass not abstract). Now i must have another class that should implement the base interface but also to add new methods (extending the default behaviour), so i'm making my second interface like this

public interface IChild : IBase {        
void MethodChild();    
}

and a class that implements this one, say ChildClass.

All good for now. Now the problem with IoC and Autofac. I want, on application start up, to conditionally register as IBase (because the project allready uses IBase in other parts) the first or the second concrete class.

The question is, in a consuming class, in its constructor, should i have a parameter of type IBase and in one of its methods should i do

(baseTypeParam as IChild)

and use one of the new methods added by IChild interface? Because if the constructor parameter is of type IChild and as i said, i register IBase with ChildClass then everything fails because, obviously, there is no IChild registered.

Or i am missing something about how to extend (design pattern?) the base interface with new methods, that the BaseClass should not implement.

Aucun commentaire:

Enregistrer un commentaire