Winforms MVP Grid Events Problem

别来无恙 提交于 2019-12-04 17:04:06

There are (at least) two variants of MVP.

  • Passive View Pattern
  • Supervising Controller Pattern

Passive View, as its name suggests, treats the UI as a more or less passive interface between the user and the application. It moves as much testable code to the presenter as possible leaving the view to handle only the most basic UI updates.

Supervising controller gives the view a little more responsibility by letting it handle data synchronization. This is usually done through data binding.

In either case event handling is accomplished by delegating to a presenter method:

EventHandler()
{
    presenter.HandleEvent();
}

If handling the event requires making changes to the form, you expose what needs to be updated as a property:

public string PropertyThatNeedsToBeUpdated
{
    get
    {
        return Control.Property;
    }
    set
    {
        Control.Property = value;
    }
}

For Passive View, grids are a hurdle. Their complexity make it cumbersome to capture all the possible events. With Supervising controller, grids are much easier since you leave data synchronization up to the data bound controls.

You have to make the judgment call as to which is more appropriate for your situation.

The key is getting all that business logic into the presenter where it's testable.

The view should call the presenter to perform the business logic, passing the information needed (e.g. the data associated with the clicked row).

The presenter then performs the business logic and updates the data.

Depending on what type of changes you need to make, that might be all you need to do, since the existing data binding might be sufficient to update the view automatically. If it isn't sufficient, the presenter can make one or more calls to the view (via its interface of course) in order to make the required changes.

As you say, you should aim to minimize the amount of non-trivial code in your view, and in particular, the view shouldn't have any business logic in it.

EDIT:

Some good general information on MVP and other presentation patterns here: http://martinfowler.com/eaaDev/uiArchs.html

You might want to check out Part 4 of the video series from Polymorphic Podcast. He uses the supervising controller pattern and shows a technique for handling data grids. The whole series was actually a good introduction for me.

http://polymorphicpodcast.com/shows/mv-patterns/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!