tags:

views:

76

answers:

2

In every form we derive from FormBaseControl, we have the following code. I'm sure there is a better way to type the controller object than this, but at the moment we have it included in every page. In the example below, base.Controller is of type BaseController, from which ExportController derives. I find duplication of this code in each derivation of FormBaseControl to not smell right, but I can't quite figure a way of righting it.

    private ExportController MyController
    {
        get { return base.Controller as ExportController; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        base.Controller = new ExportController(WebNavigator.Current);
A: 

I think there is a design problem here.

Are you sure that MyController is needed to be ExportControler (or any other) and not the base (or derivated class)? May an intermediate class, interface or generics avoid this?

Please, give details about why do you need redefinie MyControler all the time.

FerranB
There may be a design problem here, but I'm not about to redesign an application with 13k source files and currently deployed to 5k users. :-( In an export page I need access to ExportController specific members that are not available on BaseController.
ProfK
+2  A: 

Can you not use a generic class to fix this?

I.e. instead of:

 private ExportController MyController
 {
        get { return base.Controller as ExportController; }
 }

in the derived class.

Put:

 protected T MyController
 {
        get { return this as T; }
 }

in the base class, and turn the base class into a generic class BaseController<T>

Andrew Rollings
I can't change BaseController to take a generic type parameter.
ProfK
Ah. You're buggered then :)Okay. What about inserting an intermediary class that derives from BaseController that does take a generic. Then the other classes can derive from that.
Andrew Rollings
Your solution seems to confuse the controller base and page base, because MyController on the base page must return a controller. If it returns this, the as T will fail.
ProfK
In that case, I'd have to see a more complete example :) .
Andrew Rollings
It's actually not worth it, as it's almost intractable without serious workarounds, and we only create a new page every few months or so.
ProfK
There you go, Problem solved! ;)
Andrew Rollings