Why I prefer MVC

by Jakobsson 23. April 2009 13:01

I tought about writing a post about why I prefer the MVC framework instead of the old .NET way, webforms, to build my webapplications. But today when I opened my google reader to check out the new posts, I saw this post by Rob Conery which I think has some very good arguments to why you should atleast try it out.

The most common arguments I hear from webform delevlopers is that the views will look uggly with inline scripting and that you have to do it all on your own. I dont realy agree with any of those arguments, but I can see why you would think that if you haven´t realy tried it out yet. I agree that if you have alot of if statements and other logic in your view then It will look uggly and be hard to read. But you have html helpers to solve this. The view´s should be simple and not contain any logic, only display the data that you need. To me this looks alot cleaner then a webforms application with a lot of logic in the code behind.

The other argument, that you don´t have any server controls and that you have to write it all for your self, is allso a bit missleading I think. You still have html helpers that works in a similar way to server controls. Yes, a server control can be more complex and "do more stuff", but 95% of the time you use a Repeater for instance, you use it only to display data from some kind of list. And why use a repeater when you have one in c# (or you´r language)? It´s called a "foreach". You are still in control of you´r own html, so you can easily structure it any way you want.

With the MVC framework you have alot more control of the acctuall output of you´r web application. And with todays applications that depend alot on javascript its alot easier when you have 100% control of you´r html. Very simple javascript tasks can easily become pretty complex in a webform application in my experience.

Another key thing in the MVC framework is the testability. Its alot better then the options we have to test a webforms application. Even if you dont write any tests (you should!), you can still write testable code pretty easy. And in my experience, testable code is often better code. You will automaticly write more loosly couppled applications when you think about testability. Its allways easier to test small methods then big once, and you dont want to test your DAL at the same time as your BLL. This will make you seperate those two.

Ofcourse webforms hasn´t been a bad way of developing web applications all the time. But it has a few years on it´s neck now, and I think it´s time for a change. The requierements for a good applications isn´t the same anymore, and in a few years they will be different then from today. If you work as a developer you can not be affraid of changes, it´s part of our jobs.

Tags:
Categories: C#

Comments

Matt Briggs
Matt Briggs on 4/23/2009 11:26:17 PM

I think a big part of it is that when ASP.net came out, it was much much harder to do really impressive stuff with javascript. By abstracting the client side away you could do some pretty impressive things very easily.


Nowadays, it is hard to find even commercial asp.net components that measure up to a lot of javascript widgets that are freely available and a heck of a lot easier to work with. Not to mention mature javascript libraries that address short commings in the language and the platform, and make javascript a joy to work with.


I think the big reason to use WebForms nowadays is investment in it, from third party components to source code to expertise.  I find it easier and faster to get something finished with an MVC platform over webforms hands down. Webforms may be faster to get something started with, but it is FAR harder to get something finished in. 

Jakobsson
Jakobsson on 4/23/2009 11:49:24 PM

Yes, that is the point I´m trying to make here. Today when we have very powerful client side libraries, like jquery among others, it´s a lot easier to create rich interfaces when coding javascript by hand. A few years ago we didn´t have the same tools that we have today, so it made sence to let the control developers take care of that part.


I just think we need to be abit more open minded. In this line of work it´s just not possible to allways work in the same way as you have allways done. You allways need to tech your self something new to keep up with the development.

darkYuris
darkYuris on 4/24/2009 8:47:40 AM

Isn`t this 'WebForms VS MVC' thing just a marketing trick? For me it seems so.


Feeling tired from those endless 'i prefer MVC cause it`s more testable' blog posts. There should be more info about HOW to use this framework, not WHY to use it.


peace...

Jakobsson
Jakobsson on 4/24/2009 9:10:06 AM

Maby you´r right. I just think that there are some developers that just say they don´t like the framework, without even realy trying it out. But that might be because there are to few post´s about how to acctually implement it in you´r real world application.


When I first started to use the MS MVC framework, I foloowed the screencasts by Rob Conery where he builds a e-commerce site, the http://blog.wekeroad.com/category/mvc-storefront" target="_blank">mvc storefront. I think that is a good series where you can learn alot about the framework and how to use it. I do have my own searies of posts here where I build a forum application from scratch using the ms mvc framework.


But maby you´r right, there should be more post´s about how to use the framework.

colum
colum on 4/24/2009 9:30:32 AM

please change background colour.  would like to read this but dark on dark is sore on them eyes.  I don't pretend to be any design or accessibility expert but black text on a white backgound has a lot going for it and a proven history.

Jakobsson
Jakobsson on 4/24/2009 9:51:05 AM

Thank you for you´r suggestion. I´ll add some other background for the text´s. My design skills are very limited, and this is a free template from http://www.freecsstemplates.org/" target="_blank">freecsstemplates.org Smile

dave
dave on 4/24/2009 4:38:43 PM

I'm not an expert in the design field either, but I concur, it's tough to read this.  Though when I did read it, I can't say that I disagree with the basic premise.  I do find it odd that there are 'two camps' developing though, one MVC vs web forms.  In my personal opinion, using the framework to render HTML is a good idea, since it does browser caps automatically.   In SharePoint web parts, and Asp.NET on it's own, it sucks to have to check browser versions and say generate an iframe versus a div with some kind of css class, or to degrade to tables on an old piece of junk browser.  Honestly, I thought that's what frameworks are for.  Granted I'm not familiar enough with the javascript frameworks as I should be, but jquery looks like an invitation for me to get with the program. 

Jakobsson
Jakobsson on 4/25/2009 10:36:26 AM

I agree, It would be very, very nice to have a framework generating perfect html markup for you all the time, and to just have to drag a component on the scene and it will all work. The problem is that such a framework doesn´t exist. And It would be pretty hard to acctually build it. I allso like to, atleast have to option, to edit my html in the way I want. This is a option you have with the MS MVC framework and not with webforms.


You should realy try some javascript library, like jquery, because it makes javascript so much easier and so much more fun to work with.


I just think that developers should try the framework out, and form their own opinion. If you like webforms better, then use it. Then you will atleast have you´r own opinion.

Matt Kocaj
Matt Kocaj on 5/24/2009 12:40:51 PM

ASP.NET MVC rocks!


..thats all i have to say about that

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading