为什么在大多数ASP.NET MVP实现中,Presenter都附加到View事件,而不是View调用Presenter方法? [英] Why do Presenters attach to View events instead of View calling Presenter Methods in most ASP.NET MVP implementations?

查看:75
本文介绍了为什么在大多数ASP.NET MVP实现中,Presenter都附加到View事件,而不是View调用Presenter方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在Webforms MVP实现和大多数其他示例中,Presenter通常将处理程序附加到View事件.为什么视图不能直接在演示者中调用方法?只是想知道,由于将处理程序附加到事件,为特殊参数定义EventArgs的整个任务,检查事件在视图侧是否为null似乎比仅调用方法还要乏味.

I noticed that in the Webforms MVP implementation and most other examples, the Presenter usually attaches handlers to View events. Why can't the Views just call methods in the presenter directly? Just wondering, since the whole task of attaching a handler to an event, defining EventArgs for special parameters, checking if the event is null on the view side seems a lot more tedious than just calling a method.

推荐答案

当然可以,我发现这是最好的中间立场.我认为您所描述的是观察演示者风格.这样,您就可以将View与Presenter完全分离,从而使该View不会受到Presenter中更改的影响.但这也引入了测试的复杂性,这就是开始使用MVP的原因.我根本不会理会这种风格.在一个非常大的项目中,我们使用封装的Presenter样式,其中View通过IoC容器引用了Presenter的引用,而View仅在Presenter上调用方法.易于理解,易于调试,易于测试.

Sure they can, and I find that to be the best middle ground. What you are describing is I believe called Observing Presenter style. This allows you to completely decouple View from the Presenter, making the view less susceptible to changes within presenter. But it also introduces complexity in testing, and that is the reason to use MVP to begin with. I would not bother with this style at all. On very large project we use Encapsulated Presenter style, where View has a reference to Presenter, injected via IoC container, and view just calls methods on the Presenter. Easy to understand, easy to debug, easy to test.

这篇关于为什么在大多数ASP.NET MVP实现中,Presenter都附加到View事件,而不是View调用Presenter方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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