I'll explain the reason I am trying to do this first. My Initial Hierarchy:
BaseClass
|
ControlClass
| \
CalltoBase DerivedClass1
|
CalltoBase
Both call paths hit a particular method of the base class Depending on where the CalltoBase originates I need to use a different implementation of that one method. This is what I normally would have done:
BaseClass____DerivedClass2
| |
ControlClass ControlClass2
| \ | \
call DerivedClass1 call DerivedClass3
call call
The problem is that this project is growing and there will be necessity for even more implementations of that one method in the base class. Hence my reason for wanting to do something like this, I would like to only have to create 1 extra derived class instead of three every time I run into this.
BaseClass
| \______
ControlClass<t> \_____
| \ \
call DerivedClass1<t> tClass
call
Note: every line except the call lines are inheritance I do not have the authority or the time to change the system so that they would not be inheritance.
my goal is to do something like this:
public class BaseClass{code}
public class tClass : BaseClass {code}
public class ControlClass<t> : t where t : BaseClass {code}
public class DerivedClass1<t> : ControlClass<t> where t : BaseClass {code}
//defaults
public class ControlClass : ControlClass<BaseClass> {empty}
public class DerivedClass1 : ControlClass<BaseClass> {empty}
Is something like this possible or should I just accept that I'm screwed and make all of the derivative classes every time?
Edit1***************************************** After doing some more research I think my real question is: How can I use Dependency Injection to either Override a method or to set a default method when no dependency is explicitly injected?
Edit2******************************************
This code is all inside of a library that the company created and is used by many projects. It is for UAT's using selenium/specflow.
public class BaseClass
{
public T Control<T>(string controlName, string pageName = null)
where T : class
{
By sel = GetSelector(controlName, pageName)
...
return control as T;
}
private By GetSelector(string controlName, string pageName = null)
{
pageName = string.IsNullOrEmpty(pageName) ?
pageMapping[Browser.GetPage()] : pageName;
...
return selector;
}
}
public class ControlClass : BaseClass
{
public NgElement NgControl(string controlName, string pageName)
{
By sel = GetSelector(controlName, pageName);
...
return element;
}
public By GetSelector(string controlName, string pageName)
{
pageName = string.IsNullOrEmpty(pageName) ? PageName : pageName;
pageName = string.IsNullOrEmpty(pageName) ?
pageMapping[Browser.GetPage()] : pageName;
...
return selector;
}
public string PageName
{
get
{
...
return returnPage.Value;
}
}
}
public class DerivedClass1 : ControlClass
{
//SpecFlow GWT's
GivenAngularClicks(string controlName)
{
WaitForAngular();
NgControl(crontrolName).Click();
}
}
The parts that change pageName are the parts that need to be modified based on which project is the caller. It doesn't matter which derived class makes the call, what matters is the class that instantiates base class.
Shouldn't something like this be possible by using overrides, in which you call different methods on the base class? Something like: