views:

57

answers:

1

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?

+1  A: 

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.

Daniel Dyson
Good point but unfortunatly its work on an existing system so converting to MVC isn't a goer as no-one wants to payt for it!
AJM
Interesting. MVC is free. As for your time, it only takes about an hour to convert an ASP.NET app to a combined ASP.NET and MVC app. See this: http://www.codeproject.com/KB/aspnet/webformmvcharmony.aspx The change to the page should be trivial.
Daniel Dyson
I may try this for fun however I'm constrained by the client on this one even if I had a magic wand that did it instantly....
AJM
Ok. See my update in that case.
Daniel Dyson
Cool, I thought as much but thought it would be cool to avoid it!
AJM
Nice idea on the user control front
AJM