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>


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.


Need Your Help

Store objcets in singleton class

android static singleton object-lifetime

Is it good idea store my data objects in static variables of singleton class? I have class AppEngine where are some data static e.g. boolean int and instances of my classes (Configuration etc.)

Solr Data Importer is not working

solr lucene full-text-search solr4

I am trying to import data into solr from a MSSql database using Data Importer of Solr 4.0.