I have a database-driven application that runs different surveys. Most of the behaviour of each of the surveys is similar and can be represented using common patterns. But occasionally, I would like to have some special code to implement some of the functionality for a particular survey.
Now, this is done like this:
switch (SurveyId.ToLower())
{
case "surveya":
_testModule = new SurveyA(this);
await _testModule.Init();
break;
case "surveyb":
_testModule = new SurveyB(this);
await _testModule.Init();
break;
default:
break;
}
So basically with a switch identifying the exceptions.
Later in this code, I call some methods on the _testModule
class.
I understand I could do this by overriding a base implementation. Then I would not have this switch, but I would call the standard method, and have this overridden in a derived class. But that would mean the same switch comes at a higher level, calling a class that overrides the base class instead of the base class itself. Basically, it would just move this switch-up in the stack, but it would not reduce complexity.
At other points in the code in a web API, inheritance does not seem to help at all, and I see no other way than this switch.
So what is the best way to handle this?