« On Career Choices... | Main | Open-Source Forces Commoditization? »

April 19, 2005

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83455a82969e200d83441b77a53ef

Listed below are links to weblogs that reference The M/VC Antipattern:

Comments

Interesting blog. I am interested in improving, and learning more about design patterns in general. I have started a wiki to try and gather others like me.

check it out if u get chance.

http://www.codecraig.com/wiki

Interesting thoughts. I'd like to see this fleshed out into a bigger article with some examples.

Good article. You're right that following MCV is the correct way to write unit tests -- more specifically, write tests for the controllers, written from the perspective of the View code. Once the test(s) are passing then write the controller's View code.

Hope you don't mind the shameless plug, but there's a chapter on the subject (tied in with combining TDD with use case driven UML modeling) in my book "Agile Development with ICONIX Process" - more info here: http://www.softwarereality.com/AgileDevelopment.jsp

This comment by in Javalobby was worth inserting in the discussion. Here's the comment and my reply:

> To me, the concepts of model, view, and controller
> are generally agreed upon. The roles of model and
> view, are even fairly commonly defined.
>
> The role of the controller, to me, has been the cause
> for a lot of discussion.
>
> Nearly everyone seems to agree that no operations
> made in the view, should be reflected in the model,
> except through the controller.
>
> However, some prefer to view the controller as
> sitting between the view, and the model. This appears
> to me to be Benjamin's view as well. I believe this
> pattern is sub-optimal.
>
> I prefer to diagram the model, view, and controller
> as a triangle; wherein all links are bidirectional
> except between the model and view. In
> many cases, allowing the view direct, read-only
> access to the model both greatly simplifies
> the design of the controller, and greatly
> improves the performance of the view.
>
> I agree with Benjamin that a common problem is that
> developers couple the view and controller very
> tightly. This is generally a big mistake. A classic
> example being exploited all over the internet is when
> a web page validates user input before sending it to
> the server. This is particularly vulnerable to
> someone using using something as trivial as telnet,
> to mimic a browser, and provide data outside the
> bounds expected, and receiving results that were
> never intended to be released. Nearly every
> major web commerce site has fallen victim to this
> attack.
>
> Personally, I find his terminology oddly backward
> however. Rather than calling the tight view
> controller coupling problem MV/C, I think it is lot
> more intuitive to call it M/VC.

I agree; the model and view should never directly communicate. Perhaps I should've clearly stated as much in my post. Also, in no way do I believe views cannot talk directly to controllers as you suggest. How they do so is the crux of the matter.

Your point about view -> controller linkage as useful or even necessary for optimization is so true that it also true for implementation.

The standard mechanism for view -> controller interaction is registering the controller as an interested view listener (or 'observer'). Here, you'll find each view requires specific listener types, quietly leading right back to a view-specific dependency in the controller code. There's no way around this. However, the coupling damage can be minimized.

Intentionally minimize the view -> controller coupling by abstracting the listener (bridge) and have the controller return the right listener impl depending on the view it is managing (factory). This part is often forgotten but, in my opinion, one of the more important aspects of getting MVC right.

My post simply points out that coding the controller before writing the view (and calling it MCV!) naturally encourages what I describe here - at least much more so than view-first.

Thanks for the excellent comment!

Interesting. I agree with Alex. This sounds like a great topic for a longer article with some actual TDD code examples.

Hmm, the comments about prototyping and not getting a chance to refactor to fix the design/architecture sound all too familiar... the Big Ball Of Mud (http://www.laputan.org/mud/mud.html) strikes again. Will we (software designers, our management) ever learn?

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.