简体   繁体   English

MVPVM - 使用Interface或Presenter进行事件处理

[英]MVPVM - Event handling using Interface or Presenter

Handling button click, row double click and other events. 处理按钮单击,行双击和其他事件。 What is a better approach? 什么是更好的方法?

Direct Access of presenter to View. 直接访问演示者到视图。 So the view can directly call the appropriate method on the presenter, eg. 因此视图可以直接在演示者上调用适当的方法,例如。 Presenter.Save()

Or 要么

Event in interface. 界面中的事件。

interface ILoginDetailView:
{
   event EventHandler Save;        
}

View 视图

private void btnSave_Click(object sender, EventArgs e)
{
   Save(this, null)
}

Presenter 主持人

view.Save += new EventHandler(view_Save);

I feel the 2nd approach is better as it makes the view decoupled from the presenter. 我觉得第二种方法更好,因为它使视图与演示者分离。 But in all the articles/papers where the pattern is described, 1st approach is used. 但是在描述模式的所有文章/论文中,使用了第一种方法。

Personally, I always go for the second approach. 就个人而言,我总是采用第二种方法。 Generally, I don't like having the view know about the presenter, and the decoupling is a great advantage. 一般来说,我不喜欢让观众了解演示者,并且解耦是一个很大的优势。 Otherwise, I find myself making functions taking specific variables from the view. 否则,我发现自己正在从视图中获取特定变量的函数。 The event pattern breaks that nicely. 事件模式很好地打破了。

我也会采用第二种方法,因为正如你所说,将视图与演示者分离,这反过来使得这些东西可以测试。

In the MVPVM (model view presenter view model) pattern it's stated that the view is usually tightly coupled to your view. 在MVPVM(模型视图演示者视图模型)模式中,声明视图通常与视图紧密耦合。 This allows your view-model, view, and business/data access layers to remain decoupled. 这允许您的视图模型,视图和业务/数据访问层保持分离。

So once again, the presenter can be tightly to allow direct access to the view without adding responsibility to your view model or view. 因此,演示者可以严格允许直接访问视图,而不会对视图模型或视图添加任何责任。

MVPVM Design Pattern MVPVM设计模式

It's a great article and discusses MVC, MVP, MVVM, and MVPVM verly well. 这是一篇很棒的文章,很好地讨论了MVC,MVP,MVVM和MVPVM。

I think the first approach is much cleaner. 我认为第一种方法更清洁。

A Save just doesn't feel like an event . 拯救只是感觉不像是一个事件 It's a direct action. 这是一个直接行动。

In MVVM you're calling a command on the viewmodel, in MVC you're calling an action on a controller and in MVP you're calling a method on the presenter. 在MVVM中,您在viewmodel上调用一个命令,在MVC中,您在控制器上调用一个动作,在MVP中,您在调用者上调用一个方法。

A Saved or Saving is an event, but not Save . 保存保存是一个事件,但不是保存

But it's probably a question about personal preferences. 但这可能是个人偏好的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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