I have the following classes:
public abstract class StaticFileController<File, QueryData> : AsyncController
{
private string _resourceName;
public StaticFileController(string resourceName)
{
_resourceName = resourceName;
}
//This method receives _resourceName
protected virtual void DoSomething(string resource)
{
//...
}
}
public class ScriptController : StaticFileController<FileEntity, ScriptQueryData>
{
public ScriptController : base("scripts") { }
}
public class StyleController : StaticFileController<FileEntity, StyleQueryData>
{
public StyleController : base("styles") { }
}
Now, let's suppose I have this class:
public class ScriptBundleController : ScriptController
{
protected override void DoSomething(string resource)
{
// Use resource name and do something (~50 lines of code)
}
}
If I want to create a similar class for StyleController, I would have to create a new class (with the exact same implementation):
public class StyleBundleController : StyleController
{
protected override void DoSomething(string resource)
{
// Use resource name and do something (~50 lines of code)
}
}
This troubles me because it seems I am duplicating code, which will give me problems in the future.
How can I solve this problem? I want to have my implementation written only in one place. I understand that two classes would likely be needed, such as ScriptBundleController and StyleBundleController but I want the DoSomething() method to be centralized in just one class.
DoSomething()?StaticFileController.