简体   繁体   中英

MVC: Controller and View Communication (C#)

I am trying to build a program using model-view-controller.

Specifically:

I have a state manager that handles (initializing, loading, running, unloading etc... of) a set of MVCs, one at a time.

In each set, the single controller contains a reference to the relevant model and views.

Every model, view and controller are each derived from a parent class. (ie abstract model, abstract view, abstract controller). This allows me to keep much of the process in the parent classes, rather than adding duplicate code in each specific mvc set the state manager will handle.

Now the idea is for each View to call on its Controller methods, which in turn will call on its Model methods, and any resulting changes in the Model will be communicated back to the View through the Controller.

This is where I'm stuck. The only knowledge each view and model have of the controller is what is defined in its parent class. Which of course does not include the very specific methods the child-controller will need in order to properly handle the views and model.

Are there any ideas on how I can deal with this?

-Thanks.

You are probably closer to MVP than MVC. Anyway, getting around your problem may be possible using interfaces and generics.

Have your abstract classes take a generic interface:

public abstract class BaseView<TController>
{
    protected TController Controller { get; private set; }

    protected BaseView(TController controller)
    {
       Controller = controller;
    }
}

Something to that effect.

Then your specific controller will simply implement the relevant interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM