Andrew Stopford's Weblog

@poobah

News

Articles

Family

Old Blogs

MVC, such a big deal?

This is one of the posts where I should be going to bed rather than starting this but I just won't sleep if I don't post it. I was going over the comments in Ayende's post on Castle Igloo and noticed someone had posted this.

"Communication between a view and its associated controller is straightforward because View and Controller are specifically designed to work together. Models, on the other hand, communicate in a more subtle manner."

"Unlike the model, which may be loosely connected to multiple MVC triads, Each view is associated with a unique controller and vice versa. Instance variables in each maintain this tight coupling."

-- Applications Programming in Smalltalk-80(TM): How to use Model-View-Controller (MVC) http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html
  

In fact if you read Martin Fowlers essay on GUI Architecturer it captions a MVC UML diagram with

Essential dependencies between model, view, and controller. (I call this essential because in fact the view and controller do link to each other directly, but developers mostly don't use this fact.) 

So the view and controller are tightly bounded and the coupling of the two in accordance with the pattern is ok. In that context webforms meets the defination of view (aspx) and controller (code behind) . The couple is one of web coupling, it's a web framework after all but you can't easily mock events or context data and although the couple is valid it quickly stands in the way of testability.

MVC frameworks that adapt to webforms extend the controller and view to reduce the couple so tightly. The view can register to the controller with out a strict contract, in this case what occurs is that the codebehind and view unify as one view and the controller a new class. By making the controller more like a class we can inject events and mocks to it with out a web context and increase testability in doing so. We do stretch things a bit here in a pattern layers sense as the view is now taking on events and data population passing to the code behind which in turn goes to the controller class so the code behind becomes a bit of a piggy in the middle between the pattern layers. When we consider MonoRail it has true degrees of seperation between pattern layers and testability to boot, best of both worlds.

As with any pattern is using what fits for you and your project, they are not set in stone and you can adapt where you see fit.

Comments

Scott Bellware said:

We can often safely say that a given implementation isn't an implementation of such-and-such pattern if implementation doens't reflect the spirit of the pattern.

I personally don't see that the webforms' codebehind and MS-HTML file combo reflect the spirit of MVC.

Webforms also don't seem to be idiomatically in line with MVC in it's own native habitat in common .NET, which suggests to me that the pattern's objective to be a means of communication has not been achieved.

Even if webforms meets the strict definition of MVC, the strictness of the definition doesn't seem to be helpful if we're using strictness as a means to reduce meaning.

My 2 cents.

# January 29, 2007 8:46 PM

hammett said:

Andrew, I fail to see how a code behind plays the controller role. The way I see code behind is a way to not have the code on script/@server blocks on the aspx. No more no less.

IMHO a controller controls the application flow, coordinates data/view and to do so it has to be the entry point.

A code behind is only a controller if you stretch the definition of a controller. And stretch it a lot.

# January 29, 2007 10:08 PM

andrewstopford said:

Hi Guys,

Thanks for the comments. While webforms meets MVC in it's true defination I agree 100% it breaks things like seperation of concerns and testability that in turn renders the point of a pattern usless. Thats the reason webforms is extended with MVC frameworks in the first place :)

Code behind does do all the things you describe hammet, it does co-ord data to the view, it does control events fired from the view, it does control application flow etc. It's also tightly bound to the view as a inline code block would be, valid MVC. However, it must always be in context breaks testability down to such a degree that you must resort to functional testing and this miss one rung off the test ladder, not good at all. True seperation of concerns means we can introduce the correct levels of testability.

Don't get me wrong guys, webforms although standing correct to MVC also breaks the purpose of using a pattern in the first place. Don't get me started on viewstate ;-)

# January 30, 2007 4:46 AM

Mike Brown said:

What? Where in anyone's definition of MVC does it say that the purpose of the pattern is for testability? Your View is specific to the presentation technology and the Controller is specific to your View. What a lot of the comments here seem to be referencing is an Application Controller.

Michael Hunter (the Braidy Tester), suggests creating a logical interface that can support your tests as well as your UI. Something like what Martin Fowler describes in the Transaction Script pattern (http://www.martinfowler.com/eaaCatalog/transactionScript.html). That's all that most of the "MVC" frameworks do nowadays anyway. BTW, in my former life, I worked extensively in Java and the Struts "MVC" framework provided little more than ASP.Net code-behinds.

# February 8, 2007 4:57 PM

Ross said:

I have to agree with hammett. Although, codebehined is a step removed from the UI elements, codebehined at least from the ASP.NET model, is web page or page specific (ignoring inheritance).  

The true sence of MVC is to create logical reusable business rules from a UI prespective but not specific or bound to any given UI (i.e. ASP.NET, WinForms, etc).  In addition, MVC also shelters the UI from the implemenation details of any underlining BR, data access or service interface layers, etc.

# May 14, 2008 6:01 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)