模型 - 视图 - presenter中的WinForms [英] Model-View-Presenter in WinForms

查看:131
本文介绍了模型 - 视图 - presenter中的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的MVP方法是第一次,使用的WinForms。

I am trying to implement the MVP method for the first time, using WinForms.

我想了解每个层的功能。

I am trying to understand the function of each layer.

在我的节目,我有,当点击后会打开一个打开文件对话框窗口中的图形用户界面按钮。

In my program I have a GUI button that when clicked upon opens a openfiledialog window.

因此​​,使用MVP,图形用户界面处理按钮点击事件,然后调用presenter.openfile();

So using MVP, the GUI handles the button click event and then calls presenter.openfile();

在presenter.openfile(),应该认为然后委托该文件到模型层的开口,或作为没有数据或逻辑来处理,应该简单地作用于该请求并打开的OpenFileDialog窗口?

Within presenter.openfile(), should that then delegate the opening of that file to the model layer, or as there is no data or logic to process, should it simply act on the request and open the openfiledialog window?

更新:我已经决定提供奖金,因为我觉得我需要在此进一步的援助,和preferably针对我的具体点以下,让我有背景

Update: I have decided to offer a bounty as I feel I need further assistance on this, and preferably tailored to my specific points below, so that I have context.

好了,在MVP阅读了之后,我已决定实施被动视图。有效地我都会有一大堆的,将被presenter进行处理,然后委托给模型(S)的任务上一个WinForm控件。我的具体点如下:

Okay, after reading up on MVP, I have decided to implement the Passive View. Effectively I will have a bunch of controls on a Winform that will be handled by a Presenter and then the tasks delegated to the Model(s). My specific points are below:

  1. 在WinForm的负载,它必须获得一个TreeView。我是正确的思想,认为应该因此调用一个方法,如:presenter.gettree(),这反过来将委派模型,该模型将获得的数据为树状,创建并配置它,回报它以presenter,这反过来又会传递给视图,然后将只分配它,比方说,一个小组?

  1. When the winform loads, it has to obtain a treeview. Am I correct in thinking that the view should therefore call a method such as: presenter.gettree(), this in turn will delegate to the model, which will obtain the data for the treeview, create it and configure it, return it to the presenter, which in turn will pass to the view which will then simply assign it to, say, a panel?

这将是相同的WinForm的任何数据的控制,因为我也有一个DataGridView?

Would this be the same for any data control on the Winform, as I also have a datagridview?

我的应用程序,有一些模型类中使用相同的组件。它还支持插件架构,需要在启动时加载的插件。请问鉴于只需拨打一个presenter方法​​,这反过来会调用加载插件的方法,并在视图中显示的信息?这层然后将控制插件参考。请问观点持对它们的引用或presenter?

My App, has a number of model classes with the same assembly. It also supports a plugin architecture with plugins that need to be loaded at startup. Would the view simply call a presenter method, which in turn would call a method that loads the plugins and display the information in the view? Which tier would then control the plugin references. Would the view hold references to them or the presenter?

我是正确的思想,认为应该处理约presentation每一件事,从TreeView节点颜色,数据网格大小等?

Am I correct in thinking that the view should handle every single thing about presentation, from treeview node colour, to datagrid size, etc?

我觉得他们是我主要关心的问题,如果我理解的流程应该如何对这些我想我会好起来的。

I think that they are my main concerns and if I understand how the flow should be for these I think I will be okay.

推荐答案

这是我的卑微采取的最有价值球员和你的具体问题。

This is my humble take on MVP and your specific issues.

首先下,任何用户可以交互,或者只是表现出来,是的视图的。该法律行为,这种观点的特点是通过的接口的说明。具体实施只是并不重要,只要它遵守的法律 - 这个接口可以使用的WinForms用户界面,控制台用户界面,在所有的Web用户界面甚至没有用户界面(通常是测试当presenter)来实现其视图界面。

First, anything that a user can interact with, or just be shown, is a view. The laws, behavior and characteristics of such a view is described by an interface. That interface can be implemented using a WinForms UI, a console UI, a web UI or even no UI at all (usually when testing a presenter) - the concrete implementation just doesn't matter as long as it obeys the laws of its view interface.

,视图总是由 presenter控的。该法律的行为和这样的presenter特性也由接口的说明。该接口在具体的视图实现不感兴趣,只要它符合其观点接口的规律。

Second, a view is always controlled by a presenter. The laws, behavior and characteristics of such a presenter is also described by an interface. That interface has no interest in the concrete view implementation as long as it obeys the laws of its view interface.

第三,因为presenter控制其观点,以减少依赖有真正具有视图知道任何关于它的presenter没有收获。还有的presenter和视图协议的约定而这由视图界面中显示。

Third, since a presenter controls its view, to minimize dependencies there's really no gain in having the view knowing anything at all about its presenter. There's an agreed contract between the presenter and the view and that's stated by the view interface.

的意义第三是:

  • 的presenter不具有的视图可以调用任何方法,但该观点的优点是presenter可以订阅的事件。
  • 的presenter知道它的观点。我preFER做到这一点与构造函数注入的混凝土presenter。
  • 在该视图不知道是什么presenter正在控制它;它会只是从来没有提供任何presenter。

有关您的问题,您可以像这样在一定程度的简化code:

For your issue, the above could look like this in somewhat simplified code:

interface IConfigurationView
{
    event EventHandler SelectConfigurationFile;

    void SetConfigurationFile(string fullPath);
    void Show();
}

class ConfigurationView : IConfigurationView
{
    Form form;
    Button selectConfigurationFileButton;
    Label fullPathLabel;

    public event EventHandler SelectConfigurationFile;

    public ConfigurationView()
    {
        // UI initialization.

        this.selectConfigurationFileButton.Click += delegate
        {
            var Handler = this.SelectConfigurationFile;

            if (Handler != null)
            {
                Handler(this, EventArgs.Empty);
            }
        };
    }

    public void SetConfigurationFile(string fullPath)
    {
        this.fullPathLabel.Text = fullPath;
    }

    public void Show()
    {
        this.form.ShowDialog();        
    }
}

interface IConfigurationPresenter
{
    void ShowView();
}

class ConfigurationPresenter : IConfigurationPresenter
{
    Configuration configuration = new Configuration();
    IConfigurationView view;

    public ConfigurationPresenter(IConfigurationView view)
    {
        this.view = view;            
        this.view.SelectConfigurationFile += delegate
        {
            // The ISelectFilePresenter and ISelectFileView behaviors
            // are implicit here, but in a WinForms case, a call to
            // OpenFileDialog wouldn't be too far fetched...
            var selectFilePresenter = Gimme.The<ISelectFilePresenter>();
            selectFilePresenter.ShowView();
            this.configuration.FullPath = selectFilePresenter.FullPath;
            this.view.SetConfigurationFile(this.configuration.FullPath);
        };
    }

    public void ShowView()
    {
        this.view.SetConfigurationFile(this.configuration.FullPath);
        this.view.Show();
    }
}

在除了上述情况,我通常有一个基地 IVIEW 界面,我藏匿的显示()和任何老板视图或视图的标题,我的观点通常是从中受益。

In addition to the above, I usually have a base IView interface where I stash the Show() and any owner view or view title that my views usually benefit from.

您的问题:

1 当WinForm的负载,它必须获得一个TreeView。我是正确的思想,认为应该因此调用一个方法,如:presenter.gettree(),这反过来将委派模型,该模型将获得的数据为树状,创建并配置它,回报它以presenter,这反过来又会传递给视图,然后将只分配它,比方说,一个小组?

我会叫 IConfigurationView.SetTreeData(...)的IConfiguration presenter.ShowView() ,调用前右 IConfigurationView.Show()

I would call IConfigurationView.SetTreeData(...) from IConfigurationPresenter.ShowView(), right before the call to IConfigurationView.Show()

2 这将是相同的WinForm的任何数据的控制,因为我也有一个DataGridView?

是的,我称之为 IConfigurationView.SetTableData(...)为。它是由视图来格式化给它的数据。在presenter简单地服从它想要表格数据视图的合同。

Yes, I would call IConfigurationView.SetTableData(...) for that. It's up to the view to format the data given to it. The presenter simply obeys the view's contract that it wants tabular data.

3。 我的应用程序,有一些模型类中使用相同的组件。它还支持插件架构,需要在启动时加载的插件。请问鉴于只需拨打一个presenter方法​​,这反过来会调用加载插件的方法,并在视图中显示的信息?这层然后将控制插件参考。请问观点持对它们的引用或presenter?

如果该插件是认为相关的,那么观点应该知道他们,但不是presenter。如果它们都是关于数据和模型,认为不应该有任何与他们。

If the plugins are view-related, then the views should know about them, but not the presenter. If they are all about data and model, then the view shouldn't have anything to do with them.

4。 我是正确的思想,认为应该处理约presentation每一件事,从TreeView节点颜色,数据网格大小等?

是的。想想看,作为presenter提供XML描述数据和采用的数据和应用CSS样式表给它的观点。具体而言,在presenter可以称之为 IRoadMapView.SetRoadCondition(RoadCondition.Slippery)和视图,然后呈现为红色的道路。

Yes. Think about it as the presenter providing XML that describes data and the view that takes the data and applies a CSS stylesheet to it. In concrete terms, the presenter might call IRoadMapView.SetRoadCondition(RoadCondition.Slippery) and the view then renders the road in red color.

什么的点击节点的数据?

5。 如果当我点击树节点,我应该通过特定节点的presenter,然后从该presenter会制定出什么数据它需要再询问模型数据,$ P $之前psenting回的看法?

如果可能的话,我会通过需要present树中的所有数据在视图中一炮打响。但是,如果一些数据是太大,不能从一开始就通过了,或者如果它是动态的,其性质,需要从模型中最新快照(通过presenter),那么我会添加类似事件LoadNodeDetailsEventHandler LoadNodeDetails 到视图界面,​​从而使presenter可以订阅它,获取在 LoadNodeDetailsEventArgs.Node (可能经由某种其ID)从模型中,从而使该视图可以更新其示出节点详细信息的事件处理程序委托返回时。请注意,如果获取数据可能是一个良好的用户体验太慢可能需要这个了异步模式。

If possible, I would pass all data needed to present the tree in a view in one shot. But if some data is too large to be passed from the beginning or if it's dynamic in its nature and needs the "latest snapshot" from the model (via the presenter), then I would add something like event LoadNodeDetailsEventHandler LoadNodeDetails to the view interface, so that the presenter can subscribe to it, fetch the details of the node in LoadNodeDetailsEventArgs.Node (possibly via its ID of some kind) from the model, so that the view can update its shown node details when the event handler delegate returns. Note that async patterns of this might be needed if fetching the data might be too slow for a good user experience.

这篇关于模型 - 视图 - presenter中的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆