Annoying duplication in derived classes

In each form we get from FormBaseControl

, we have the following code. I'm sure there is a better way to typeset a controller object than this one, but for now we've included it in every page. The example below base.Controller

is of the type BaseController

from which it is inferred ExportController

. I find this code duplicated in every expression FormBaseControl

so as not to smell to the right, but I can't figure out exactly how to fix it.

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

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

      

0


source to share


2 answers


Can't you use a generic class to fix this?

those. instead:

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

      

in a derived class.



Placed:

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

      

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

+1


source


I think there is a design issue here.

Are you sure MyController is needed for the ExportControler (or whatever) and not for the base (or derivative) class? Can you avoid middleware, interface or generics?



Please give details of why you need to redefinie MyControler all the time.

0


source







All Articles