views:

101

answers:

4

I have been studying, playing with and working with ASP.NET MVC since Preview 1 in 2007 (december). I have been a fan of it since 2008 and I support it all the way.

However I keep hearing and reading "ASP.NET MVC enforces a strict separation of concerns", including reading it in Professional ASP.NET MVC 1.0 by Rob Conery, Scott Hanselman, Phil Haack and Scott Guthrie.

What I don't understand is the enforcement. What if I define my controller with actions with nothing but

return View();

and in the view (aspx view engine) I do everything? (data fetching, business logic, decision making, rendering etc.)

How does it (ASP.NET MVC) ENFORCE me to separate the concerns?

I think this is a blatant overstatement and it should read "suggests a separation of concerns".

Can you convince me it enforces?

+7  A: 

The primary thing is that it start off on the right foot - i.e. a separate controller and view. If you choose to do something crazy, nobody will stop you. Except hopefully peer reviews. And common sense.

But contrast to webforms, where you have to try very hard not to mix the concerns.

Marc Gravell
Good point, I agree.
Andrei Rinea
Not to mention all those pesky SqlDataSource controls which tempt you to do bad things.
Andrei Rinea
+2  A: 

Sure, you can do everything in the view. You'd just be a terrible programmer, and wouldn't be following the MVC pattern at all.

I'm also pretty sure there are some things you cannot do in the view at all, only in the controller. I may have my .Net and Rails confused, though.

Matt Grande
What are the things that I **cannot** do in the view? It is part of my question..
Andrei Rinea
I don't think ViewState's around. That's something you cannot do. :)
rball
That is an unquestionable improvement, I agree.
Andrei Rinea
+3  A: 

I don't think it enforces it, so they probably mispoke. Like you said, "suggests" or "promotes" is a better word.

rball
Or perhaps "enables"
Marc Gravell
Thank you. Well in "Professional ASP.NET MVC 1.0" by 4 of the main guys at Microsoft on page 86, row 27 states "One of the defining characteristics of the MVC pattern is the *strict separation of concerns it helps enforce* between the different components of an application.
Andrei Rinea
I think the key there is *helps* enforce.
Matt Grande
I agree with Matt, you'd have to go out of your way to do something like put a GridView in there.
rball
I think the same way that RoR is "opinionated", you could probably think that it gives an "opinion"
pageman
+2  A: 

What mechanism would you suggest for "enforcement" of the kind you want? Should the framework issue warnings when the cyclomatic complexity of View code exceeds some threshold? Should the framework run something like lint on your source and slap you on the wrist when you do un-MVC things (whatever those are)?

More to the point, what would be the benefit of all that finger-wagging? Aren't we trying to write applications that please our users?

DDaviesBrackett
Seeing all the newbies writing bad code that some of us need to work with or rewrite, yes, I would like something that slaps your wrist.
Andrei Rinea
The reason I asked the question of how you wanted that wrist-slapping implemented was, I suppose, to illustrate the difficulty of doing so in any meaningful way.
DDaviesBrackett
A compiler-time warning would be a step ahead..
Andrei Rinea