MVP:第3方控件,视图层可以放多少逻辑

发布于 2024-10-09 13:14:40 字数 1359 浏览 3 评论 0原文

我正在学习MVP模式,但仍然有一些疑问。

Martin Hunter 在他的 MVC/MVP 概述中写道:

在 MVP 中,视图成为一个超薄组件,其目的纯粹是向用户提供演示。视图捕获并处理用户引发的事件,但将这些事件直接转发给知道如何处理它们的演示者。

(...)

但是,使用 MVP,视图会捕获引发的事件并将其转发给控制器(演示者)

这对于按钮和文本框来说没问题,但是如果有一些更复杂的控件怎么办?假设我正在使用第 3 方组件,例如 Devexpress 的 TreeList 控件。假设我想在用户单击展开按钮“+”时动态构建子节点。不使用任何模式,我可以这样编写代码:

private void BeforeExpand_EventHandler(object sender, BeforeExpandEventArgs e)
{
    TreeList treeList = sender as TreeList;
    MyModelObject nodeObj = e.Node.Tag as MyModelObject;

    treeList.BeginUnboundLoad();
        
    //Create sub-nodes depending on nodeObj 

    treeList.EndUnboundLoad();
}

如您所见,有一些视图对象,例如 BeforeExpandEventArgs、TreeListNode,以及一些特定操作,例如 BeginUnboundLoad() 等。在这种情况下,我的视图层不能是“超薄”。我无法直接传递给 Presenter 对象(例如 BeforeExpandEventArgs),因为它会影响 Presenter 的某些 View 内容。

我的问题是:我可以在视图层中放入多少逻辑?例如,下面给出的代码可以吗?

private void BeforeExpand_EventHandler(object sender, BeforeExpandEventArgs e)
{
    TreeList treeList = sender as TreeList;
    MyModelObject nodeObj = e.Node.Tag as MyModelObject;

    treeList.BeginUnboundLoad();
        
    e.Node.Nodes = this.presenter.GetNodes(nodeObj);

    treeList.EndUnboundLoad();
}

I'm learning MVP pattern, but still have some doubts.

Martin Hunter in his MVC/MVP overview wrote:

In MVP, the view becomes an ultra-thin component whose purpose is purely to provide a presentation to the user. The view catches and handles events raised by the user, but forwards these directly to the presenter who knows how to deal with them.

(...)

However, with MVP the view catches events raised and forwards them to the controller (presenter)

This is fine with buttons and text boxes, but what in case there are some more complex controls? Lets say I'm using 3rd party components, like Devexpress's TreeList control. Assume I want to dinamically build sub-nodes when user clicks expand button "+". Not using any pattern I could code this like that:

private void BeforeExpand_EventHandler(object sender, BeforeExpandEventArgs e)
{
    TreeList treeList = sender as TreeList;
    MyModelObject nodeObj = e.Node.Tag as MyModelObject;

    treeList.BeginUnboundLoad();
        
    //Create sub-nodes depending on nodeObj 

    treeList.EndUnboundLoad();
}

As you can see there are some View-objects, like BeforeExpandEventArgs, TreeListNode, some specific actions like BeginUnboundLoad(), and so on. In that case my View layer cannot be "ultra thin". I cannot pass directly to Presenter objects like BeforeExpandEventArgs because it would affect the Presenter with some View stuff.

My question is then: How much logic I can put into View layer? For example, is code presented below ok?

private void BeforeExpand_EventHandler(object sender, BeforeExpandEventArgs e)
{
    TreeList treeList = sender as TreeList;
    MyModelObject nodeObj = e.Node.Tag as MyModelObject;

    treeList.BeginUnboundLoad();
        
    e.Node.Nodes = this.presenter.GetNodes(nodeObj);

    treeList.EndUnboundLoad();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-10-16 13:14:40
  1. 我认为View负责处理这个问题。MVP(MVC)的思想是你可以在不改变Model和View的情况下切换到其他View。至少从理论上讲;)。这样视图逻辑就可以像您一样保留在视图层。
  2. 我不同意你的示例中的一件事是对演示者的引用。你应该做相反的事情。演示者必须引用视图和模型对象。在这种情况下,View 调用 Presenter 已经侦听的事件,并且 Presenter 将节点数组返回给 View。
  1. I think the View is responsible for handling this issue.The idea of MVP(MVC) is that you can switch to other View without changing Model & Presenter at least theoretically ;). So that the View logic may stay at view layer as you did.
  2. One thing I don't agree in your sample is a reference to the Presenter.You should do the opposite. The presenter has to have reference to View as well as Model objects. In this case the View invokes its event which Presenter has already listen and Presenter returns array of nodes to View.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文