当前位置 > 首页 > Asp.net

MVVM Mediator Deluxe

2009-10-7 10:53:00来源:Asp.net

From what I’ve been reading on the implementations of the Mediator pattern for MVVM, I must say I’m a little disappointed. Maybe it’s my background with a certain UI design methodology (for which I must credit Philip Almey, a former colleague) for the last five or six years. Using (hierarchic) state charts to design user interfaces allows you to think about it’s behavior without being hampered by technology nor content or style. I’m happy that this “way of thinking” still stands firm as technology progresses (into Silverlight/WPF that is). Translating such charts into the asynchronous code flows that rule the “new world” is a challenge. One that requires clear separation of concerns, and the MVVM pattern adds a lot of value to solve this challenge. Another piece of the puzzle is the EventAggregator (Prism term) / Mediator / Message Bus / Messenger that provides the communication mechanism between your views and view models. But current implementations seem to focus on using the mediator as a publish/subscribe pattern vehicle and going to great lengths to eliminate memory leaks (which I personally find a little over the top considering that “unsubscribe” should be something explicit).

Looking at the mediator communication mechanism from a messaging perspective, I see a lot of room for improvement. There is no explicit concept of addressability (think WS-Addressing). If I want to send a message to a specific view, I’ll have to juggle with predicates and properties to make it work. Mind you I’m not advocating for tightly coupling views and view models (although some implementations already do this on subscription ;-)). After all, for addressability, one can simply use System.Uri. Along with addressability comes explicit support for the request/response message exchange pattern (alongside the existing publish/subscribe pattern). I’d like to see an API along the following lines:

//Receive any message of the specified type
IDisposable Accept<TRequest>(RequestCallback<TRequest> callback) where TRequest : RequestMessage;

//Receive messages of the specified type matching the predicate
IDisposable Accept<TRequest>(Expression<Predicate<TRequest>> predicate, RequestCallback<TRequest> callback) where TRequest : RequestMessage;

//Reply to a request message
void Reply<TReply>(TReply reply) where TReply : ReplyMessage;

//Send a request message
void Request<TRequest, TReply>(TRequest request, ReplyCallback<TReply> callback)
    where TRequest : RequestMessage
    where TReply : ReplyMessage;

Remark: People who have used WSE3 (yes, really) will know that you can use the message to specify the addressing bits.

I know this API is more rigid (contrary to sprinkling Action<T>) but that’s also its strength. It forces you to think in terms of request/reply messages that flow around. This is how I discovered that sequence diagrams are the new hype all over again to model the communication between views and view models and how they help me understand the message flow. I realize that there are a lot of details and mechanics about the API that I haven’t explained, but this post is just intended to get the juices flowing.

My conclusion: Is it impossible to achieve the above with the current incarnations? No, but it’s cumbersome and too verbose.



  • 时间显示控件(yyyy-MM-DD HH:MM)

    2007-2-25 21:05:00

    摘要: 在各种项目里都少不了时间的出现,有时候我们要选择时间,但有时候只能选择年-月-日 没有时分,有时候为了处理这种时间显示还要花点时间来处理,在这种情况下就写了一个简单的时间控件: 下面是代码

  • 【ASP.NET】--web.config中的连接字符串

    2012-5-11 18:34:00

    web.config中的连接字符串,与大家分享。 在ASP.NET的web.config中,可以用两种方式来写连接字符串的配置。 < configuration >

  • Microsoft CRM 3.0 Mobile安装手记(三)——Mobile端准备

    2007-3-2 13:53:00

    由于没有移动设备,我使用模拟器进行模拟。 首先安装配置模拟器环境。安装 Windows Mobile 5.0 Emulator Images for Pocket PC - CHS.msi

  • A Small World in ASP.NET–NimblePros Acquired by Telerik

    2012-4-27 2:39:00

    A few weeks ago, fellow ASPInsider Steve Smith of NimblePros passed a client to me. It’s a part

  • 本周ASP.NET英文技术文章推荐[03/25 - 03/31]

    2007-3-31 10:18:00

    摘要: 本期共有6篇文章: 1.ASP.NET AJAX:客户端事件查看器 2.JavaScript和.NET中的JavaScript对象标记(JSON)介绍 3.在ASP.NET 2.0

  • 也谈.NET MVC 2 + ExtJS的部署问题

    2012-8-4 0:50:00

    由于业务需要,笔者刚进到一个项目组,由于没有美工,前台采用ExtJs + MVC 2 ,迭代1的项目做的还算比较顺利,至少在本机上是运行没有任何问题的, 但是为了给客户演示,我兴高采烈的将网站部署

  • MVCTurbine and my response

    2009-12-15 23:20:00

    As mentioned in my last post’s comment, Javier has posted his thoughts . Though I criticized his

  • 步步为营:三层架构+ASP.NET MVC 架构的快速搭建(入门篇)

    2012-7-6 14:04:00

    上一篇博客中《 两天完成一个小型工程报价系统 》,许多朋友向我讨源码。其实我之后没发了,确实那种三层架构没什么意思,只是我个人的孤芳自赏,很多的处理都不是很成熟。所以我重新架构了一番,以一个小例子来

  • ASP.NET 高级编程基础第六篇—开发原则1

    2012-8-12 19:41:00

    前言: 从这篇我们开始学习web开发原则,所谓没有规矩,不成方圆,在我们编程这个领域也是一模一样的,我们需要对我们的编程设计很多规则,当然这些规则都是你经过了这么多年提炼出来的我们必须注意的事项,如

  • web工作流管理系统开发之十 数据库连接及事务设定

    2009-11-24 16:55:00

    阅读: 311 评论: 1 作者: web报表 发表于 2009-11-24 15:03 原文链接 为了方便设置数据库连接和事务的一致,将所有数据库连接信息统一设置在fccon