I have a generic editor in ASP.NET page for editing lookup values for different database tables.
The code behind uses a base class which handles 99% of the work across all the different lookups in the system.
A C# class is passed in to the base class from the code behind as below.
public class LookupPageBase : System.Web.UI.Page
{
protected IEditableLookupManager LookupManager
{
get
{
return this.lookupManager;
}
set
{
this.lookupManager = value;
}
}
}
public partial class LookupsEditor : LookupPageBase
{
this.LookupManager = new ConcreteManagerClass();
}
A different C# class is passed into the lookup Manager property for each lookup. I could use the factory pattern to avoid a big if then else in the code behind. However, I was wondering if the same effect could be achieved via a subclass of the code behind e.g.
public partial class LookupsEditorSubClass : LookupsEditor
{
public LookupsEditorSubClass() {
base.LookupManager = new ConcreteManagerClass();
}
}
Questions:
1) This would require the code behind class to be dynamically set….. Can the code behind class be dynamically set and is it even possible to inherit from a partial class?
2) If using a factory instead do I just need to accept a big if then else?
Any chance that you could use MVC for this? I see that you have included .NET 3.5 in the tags, so there is no real technical reason why you shouldn’t.
The reason I ask is that this problem would be easily solved using MVC. Only the model would change between your different cases.
Update: Following your comments, I think that the Factory approach is probably best for you. Messing around with the codebehind classes has resulted in nothing but trouble for me in the past. Rather than changing the codebehind class, why don’t you use usercontrols for the editing. Then, you could load a different usercontrol depending on the particular requirements of the entity that you are editing. This could be provided by a factory.