当我们谈论MVP的时候,我们在说些什么-14条军规

下面我简单翻译的关于MVP设计模式中的14条准则:

  1. 所有的View都应该以View后缀结束:TaskView/ITaskView
  2. 所有的Presenter都应该以Presenter后缀结束:TaskViewPresenter
  3. 让Presenter来处理所有请求,View仅处理GUI的操控细节。换句话说,就是View不处理具体的业务,应该交由Presenter来处理,View仅仅响应用户操作,以及完成数据绑定。
  4. View对Presenter方法的调用应该像事件触发一样:OnXxx()
  5. View对Presenter的调用应该尽量少,而且只能是第4点的调用方式
  6. 禁止通过Presenter直接访问Model或者Service取得结果,Presenter的方法都应该是无返回值的。这样可以保证View不是从Presenter拉去数据,而是有Presenter主动将结果推送给View。
  7. Presenter对View的调用,应该仅通过接口。每个View都应该对应一个IView, Presenter只依赖IView,不依赖实现。这样可以在单元测试的时候Mock View。
  8. View中除了IView中定义的方法之外都应该是非Public的。
  9. 除了Presenter,禁止从其他的地方直接访问View。
  10. IView中定义的方法应该根据use-case取一个具有业务含义的名称,像SetDataSource这样意义模糊的命名是不合适的。
  11. IView中不要只定义方法,不应该定义属性。Presenter对View调用应该通过方法的调用,而不是Set Data。
  12. 数据应该保持在Model中。
  13. View中方法名不应该包含具体的控件名,这会导致Presenter知道太多的View的实现细节。
  14. View中的方法名应该具有业务含义,这样代码更容易被理解和自描述。

个人观点

  • 第4、5点说的是View怎么调用Presenter的方法,目的是View传递用户的请求给Presenter。个人的观点更为激进, 我觉得应该杜绝View对Presenter方法的调用。
    那么,View应该如何调用Presenter呢?没错,event。View一定是被用户触发了某个事件,才需要请求Presenter的,同时这个事件就是某一个业务。那么我们应该在IView中定义好相应的event,然后在Presenter中订阅相关的事件即可。
  • 第11点, 个人觉得在IView定义属性也未尝不可。Presenter往View推消息当然是用方法更有语义。但是有的时候,Presenter收到某些来自View的请求的时候,我们同时也需要一些View上其他Data。
    也就是说,有的时候我们同时要对View获取或推送Data,那是不是可以考虑直接定义成属性呢? 当然这是一种方式。
    如果严格执行第11点的建议,我们也可以用参数将Presenter需要的数据传递给Presenter。具体来说就是将就是通过event的EventArgs来传递,这是另一种方式。

原文:Design Rules for Model-View-Presenter
In my current project the MVP pattern is used in the supervising controller mode. The MVP pattern is an adaption of the old MVC pattern that incorporates that the capabilities of WinForms views have become smart enough to lift some of the burdens previously implemented in the controller. This applies to e.g. handling click events and data-binding; a presenter only injects the model into the view which exploits data-binding, while a controller explicitly sets the values of each control in the view. In short, a presenter should handle the use-case logic only, not the view logic.
Each view must implement an interface that defines all interactions that the presenter has with the view. This ensures a clear separation between the presenter and the view, and has the advantage of making the presenter easy to unit-test using mocking. The view never calls other modules and services directly; it must always use an event to request that the presenter provides the needed data or service.
This project uses CAB/SCSF to generate the view modules. The generated code is not quite as I would like it, I prefer that the view has no knowledge of the presenter and no direct access to it either as this gives a cleaner separation between views and presenters. I prefer that the view interface defines a set of events that is used to communicate view events to the presenter. This makes it harder for unknowledgable programmers and code monkeys to misuse the presenter reference; making shortcuts and ignoring the reasons for using patterns is very common and leads to unmaintainable code. Read Jeremy Miller's View to Presenter Communication for further opinions on this topic.
The following MVP design rules have a CAB flair, but are generally applicable to any MVP implementation:

  1. All views should have a XxxView suffix: TaskView/ITaskView
  2. All presenters should have a XxxPresenter suffix: TaskViewPresenter
  3. Let the presenter do all use-case processing, but keep GUI control details in the view
  4. All presenter methods called by the view must start with OnXxx() as they are events by design (MVP)
  5. Calls from the view to the presenter should be kept at an absolute minimum, and used for "event" type calls only: _presenter.OnViewReady();
  6. It is FORBIDDEN to use the presenter reference to access the model or services directly, no return values from presenter methods
  7. Calls from the presenter to the view MUST go via the interface only
  8. No methods in the view shall be public unless they are defined in the interface
  9. It is FORBIDDEN to access the view from anywhere but the presenter, except for loading and showing the view from the CAB ModuleController
  10. The interface methods shall have long meaningful names (not "SetDataSource") based on the domain language of the use-case
  11. The interface should contain methods only, no properties - afterall the presenter drives the use-case by calling methods, not by setting data
  12. All data in the MVP component should be kept in the model, no data should exist only as properties of UI controls
  13. The methods of the view interface should not contain UI control name references (e.g. AddExplorerBarGroup) as this makes the presenter know too much about the implementation technology of the view
  14. Stick with domain naming in the view methods (e.g. AddTaskGroupHeader), this makes the code easier to understand and the tests self-describing
    Rule 11 and 12 might surprise you if your perception of the model is that it contains business entities only - traditional database-driven design thinking. The model is rather business process documents that are involved in the use-case, i.e. domain entity and value objects. Even views used for searching have a model, it is a specification or query object. Jeremy has some guidelines on assigning responsibilities in MVP.
    Rule 6 helps adhering to the "tell, don't ask" principle - properties make it far too easy to ask, rather use methods on the view to tell it what to do. I've seen design-time view data-binding directly against presenter properties, not exactly separation of concerns. Neither is it very testable, as there is no interaction from the presenter through the view's interface when the view asks the presenter directly. When mocking the view, there will be no recorded / testable interaction.
    Using long meaningful names in the view interface has the nice side-effect of making the presenter unit-tests a kind of mini documentation.
    I wish there was a FxCop Contrib project on CodePlex like for EntLib, CAB/SCSF and ASP.NET MVC. Then maybe I would write and publish some of these MVP design rules as custom FxCop rules.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容

  • 内容作为新媒体运营非常重要的一部分,让很多人恼得恨不得撸光了头发。做原创?都说原创才是最棒哒,容易形成差异化,那么...
    文中老湿阅读 1,557评论 0 1
  • 在昌平路上的“小说咖啡馆”,看了一部1983年的法国电影《关于我们的爱情》,导演是莫里斯·皮亚拉。这部电影真“法国...
    萌之刺刺阅读 5,249评论 0 2
  • 有些问题是需要语言来解决的,有些问题是需要暴力来解决的,有些问题可以通过和平的方式来解决,有些问题必须用武力才能见...
    李一十八阅读 362评论 0 0
  • 遇见是两个人的事,离开却是一个人的决定,遇见是一个开始,离开却是为了遇见下一个离开。这是一个流行离开的世界,但是我...
    大宝妈咪娜娜阅读 386评论 0 1