MVP:第3方控件,视图层可以放多少逻辑
我正在学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)