ASP.NET Webforms vs ASP.NET MVC

I thought about writing on this subject when i felt lots of people are confused about ASP.NET MVC. Some really asks why we need MVC when we have asp.net webforms. I know most of them never followed any other architecture other than 3 tier. Most of them 3 tier is life. They considers 3-tier is similar to MVC, so how come we need another framework like MVC.

At some point i also confused why people are comparing 3-tier with MVC.

3 tier can’t control the flow right. Components are one way or other related to the other. Then we could make it a 3 tier pluggable using the Inversion of Control and Dependency Injection(IoC & DI) architecture, but IoC and DI is just a way to supply objects /inject the objects on demand. In IoC & DI also components are strongly  depending on one another, but through interfaces implemented on each components.

Each architecture has it’s own purpose and benefits. It’s true that MVC is a very old pattern, founded in late 1970 or some year after that. But still it proves to be the best in, architectural pattern – since we know what exacts happens in each places (Model, Controller and View).

Small History of MVC

MVC was invented at Xerox Parc in the 70’s, apparently by TrygveReenskaug. I believe its first public appearance was in Smalltalk-80. For a long time there was virtually no public information about MVC, even in Smalltalk-80 documentation. The first significant paper published on MVC was “A Cookbook for Using the Model-View-Controller User Interface Paradigm in Smalltalk -80”, by Glenn Krasner and Stephen Pope, published in the August/September 1988 issue of the JournalOfObjectOrientedProgramming (JOOP).

That’s just history of MVC architectual pattern. our discussion goes to asp.net mvc and asp.net webforms.

In recent week i was been interviewing one of the candidate for an open position in the company i am working. The candidate was so confused with MVC, he was referring ASP.NET MVC to 3 tier.

It’s true layer logic wise 3 tier and MVC are similar, but MVC has more duties than the 3 tier does.

MVC                       3 Tier

View                  Presensation Layer

Controller           Business Logic

Model                Data Access

This is the comparison most of them makes. It might be true that on the base functionality we might can compare this way. But in my sense, may be that’s the mistake others makes, since they mostly work on 3 tier / n-tier architecture, when they try to compare it with MVC, they feels both are same.

But Logically and functionaly thinking/comparing for me and i believe for most others too, these are not similar.  These are entirely different architectures. So my advice is don’t ever compare with 3-tier and MVC. Try to learn and work on MVC, as MVC itself.  That would give you a better understanding.

To my understanding and finding this is the real purpose of each compents in MVC.

MVC                       Purpose/Role

View                      View Logic / UI Logic

Model                    Business & Data Access Logic

Controller               Application Flow Logic ( Controls flow of the Application)

I hope this makes clear that MVC and 3-tier are no way similar.. I would suggest reading the following slideshow, to have more information on ASP.NET MVC.

I present you the well known Stephen Walther’s presentation slide for Tech-ed 2009.

Ok. Lets discuss about the question, why we need asp.net MVC when we have ASP.NET Webforms?

As a solution developer my main concern would be unit tests, complexity and the scope and structure of the project.

When you are developing an application modern world using ASP.NET webform, i cannot apply unit test efficiently. I can apply unit test on Business Logic , data access logic classes through some variation on my design or using some 3rd party unit testing applications.

But main aspect is Can i unit test my ASP.NET webform? Webforms are event oriented right. Can i unit test on a particular  EVENT and capture the output of the page?. The  answer is a big NO?. In ASP.NET WebForms we have no control on how a control is rendered or when it is rendered or what event will be fired?.. NO,  NO don’t think wrongly i am telling in the sense of Unit Testing and Behavioural. another major problem is VIEW STATE and some junk of HTML generated by ASP.NET Webform, when a page is large, that does make any sense, how much space the VIEW STATE is going to take in Rendered HTML content to Browser. The larger the ViewState, the slower the page rendering.

Some of the ASP.NET Web Controls renders junk HTML code, on which we have no control.

That’s why we go for ASP.NET MVC.

in ASP.NET MVC, we can control

1.) How exactly should the application should flow.

2.) What data should be passed to whom and when?

3.) No View State and bunch of junk codes. We controls the HTML of the page. View renders the page in the way we mentioned, nothing extra will be added.

4.) Since ASP.NET MVC are based on ACTIONS, we can easily apply unit test on an ACTION and capture the output.

5.) Components are Loosly coupled. No direct dependency, you can unit test each components separately.

6.) Whole application flow will be controlled by a CENTRAL COMMAND CENTER called CONTROLLER in ASP.NET MVC

7.) REST (Representational State Transfer) based URL’s.

8.) SEO ( Search Engine Optimized ) friendly URL’s.

…. and more…..

So that’s goes some of the basic features of ASP.NET MVC

Like most development processes MVC is NOT a good fit in all cases. There will be times where developing using the MVC framework will not be the best choice of development. It will be more of a project by project basis if MVC is the right fit.

MVC is better if:
It is a long duration project with requirements that may change over time.
If your team has separate UI developers and back end developers.
If you need to have unit tests for all actions.

Web Forms are better:
If there is no separation between UI and backend developers.
If the project is small.
If you already have a DAL.
If you have a library of self contained user controls.

Basically, if it is a large and complex project, then MVC is the way to go. If it is a small, one off, short duration project ASP.NET Forms will be faster. But a small project may be a good opportunity to test drive on MVC.

So here i conclude this article. It’s depending on you and your project whether you want to go for ASP.NET MVC, before making decisions, please go on with a feasibility options study of your project. i strongly recommend that. In future ASP.NET MVC 2, will have more features than ASP.NET MVC 1.0 had. So always choose ASP.NET MVC 2, if you are newly going on with develop an application using ASP.NET MVC..

Some of the information on this article i fetched from google search. So i thank all of those who provide the substantial information on the internet for preparing this post.

Happy Coding!!!!!