Annoying duplication in derived classes

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);

Answers


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>


Need Your Help

Adding Multiple GET Requests to NSMutableArray in iOS App

ios objective-c json nsmutablearray nsurlconnection

The app keeps score during a game. Based off of your score, it will retrieve a quote from an online database, using a GET method and returning it in JSON format. For example, your score is 5, you...