views:

270

answers:

8

Hi all

Ive been doing a bit of research / reading into mvc recently and was just wondering what the main purpose is.

  1. is it as some people say to seperate the logic from the html
  2. or to create clean url's

i could be missing the point completely, but asp.net forms really seperates the logic from the html and if you just want clean url's why not use a mod_rewrite rule?

A: 

MVC existed long before people tried to use it in HTML pages. The main reason for MVC is to get a grip on the logic to drive your application. MVC allows you to clearly separate things that should be separate: The model, code which converts the model value for the display and the code which controls the model.

So this is not related to HTML or URLs in any way. It's just that MVC makes it really simple to have clean HTML and simple URLs.

Aaron Digulla
Please clarify "The model, code which converts the model value for the display". What's the model value?
Matt
The model is your internal representation of the reality. For example, the model of a person would have attributes like "name", "address", etc. The "view" converts the value (for example an address) for the display (i.e. it adds the necessary HTML, escapes illegal characters, etc).
Aaron Digulla
+2  A: 

MVC is a design pattern. Its purpose is to separate business logic and presentation details.

ASP.Net MVC is a mechanism to create web applications using ASP.Net and the MVC pattern. One of the features of ASP.NET MVC is the ability to use SEO friendly URLs to provide commands to the controller part.

You can do as you have stated but ASP.Net have provided you a mechanism to do this easier.

The way ASP.Net Webforms was designed is that it made it easy for you drag controls on to the web form and code the logic underneath. ASP.Net MVC is designed so you separate your concerns easier.

John Nolan
+4  A: 

MVC is a software engineering concept which is used more widely than just in ASP.net.

In a nutshell it encourages strong separation of:

  • business logic (the Model) the code which does all the brute force work behind the scenes; dealing with the database, performing large calculations; and
  • user interface logic (the View) the code which presents information to your users in a pretty way.

The C is for Controller - the ligaments that bind the bones of the model and the muscles of the views and allow them to communicate with each other cleanly.

You are correct that 'normal' ASP.net uses code-behind files so that page markup is kept separate from the code that generates that markup (in contrast to languages like PHP where code is embedded directly amongst HTML), but MVC ASP.net encourages even more separation in the ways I described above.

Take a look at this tutorial for a more detailed discussion of the pattern. Also take a look at this SO question


The MVC pattern has nothing to do with rewriting URLs. ASP.net MVC might make this easier but that is not by any means it's main purpose.

Mark Pim
A: 

The URL part of the ASP.NET MVC framework is just a modern phenomena to produce search engine friendly urls. They've infact been around long before the Microsoft team decided to add them to the framework (which required IIS7 before it could be done with no IIS extension).

The greatest pros in my view come from being able to test more easily, and separating off the parts of your application more cleanly. The whole ActionResult architecture of the ASP.NET MVC framework makes it very easy to switch from AJAX to plain out POSTs.

Delphi 5 use to employ the MVC model for its ISAPI extensions, 10 years ago.

Arec Barrwin
+1  A: 

MVC is not just an ASP.net thing, it is a design pattern that was widely accepted before it was created within the .NET framework, the thing about MVC is the separation of data from presentation(user interaction) from the business layer. It was just a way for Microsoft to offer that type of design pattern under the .NET framework

TStamper
also a way for Microsoft to address things that wasn't so much available in Webforms
TStamper
+3  A: 

Testability is a big benefit of using ASP.NET MVC. It is non-trivial to write unit tests for ASP.NET winforms. It is much easier to unit tests for Controllers.

If you are doing MVC correctly, your views should be very light, and a lot of your logic is implemented in the Controllers.

Linus
+1  A: 

Although guys before me already give enough answers to the queston of purpose of ASP.NET MVC there is one thing I would like to add.

The ASP.NET Web Forms tried to abstract html and web from web development. That approach lead to the lacks in performances and usage of rich javascript frameworks.It was possible to create web application without actual knowledge of the web.

And to answer to you initial question, the purpose of ASP.NET MVC, I'll quote Dino Esposito:

With ASP.NET MVC, you rediscover the good old taste of the Web—stateless behavior, full control over every single bit of HTML, total script and CSS freedom.

Misha N.
+1  A: 

Let me compare the two for you:

Asp.net web forms

They matured the old ASP technology that was much more like PHP. Code and presentation were piled up in the same file. Asp.net web forms upgraded this model by providing a mechanism of separating the two. But they built on top of the good things that windows application developers had. The drag drop interface creation with control events just like they exist in a windows application.

Therefore it was rather easy to start developing on Asp.net web forms. But non savvy developers soon got to a bottleneck they didn't know existed (like slow postbacks due to huge view state etc.). Technology used some tricks to make this work. But on a serious large scale application this became quite a problem. Developers had to mingle their code to make it work with Asp.net web forms framework. Their complex forms had complex codebehinds with hard maintainable code with complex state.

The good (as well the bad) thing were at that time rich server cotnrols. Nowadays with web 2.0 they don't seem rich anymore since they don't actually support client side functionality as much as they should. So Microsoft decided to also cram in something else. Update panels. That made partial rendering (and Ajax) possible with almost a flick of a finger. But it came with a cost. Everyone that used (uses) it soon realised it's not a viable solution that a professional application could implement.

Asp.net MVC

Now we have a new technology that doesn't have much in common with Asp.net web forms except for its first part of the name. MVC framework actually does separate code from user interface (view). Controller actions (code that executes on any HTTP request) is kept small and doesn't do anything with visualisation (it doesn't bind data to certain controls etc.). Controller action barely prepares data for the view to either consume or not. It's up to the view.

Views on the other hand just display and provide data. They can be partially or fully rendered. They support Ajax functionality to the point that everyone would like to use. Actually everything is separated into basic little things. Divide et impera (divide and conquer) seems to be the save-line here.

There's not hidden functionality. No flirting with windows development. It pure request response framework. Developer has the ability to 100% control the visual aspect of their app. But for the cost of not having rich controls out of the box. Those may be provided by the community or some developers prefer to create per purpose controls that serve the process much better.

Which one is better then?

Both have their pros and cons. But if you decide to build a semi complex, modern and maintainable application I'd suggest you give MVC a go.
But if all you need to do is a 15 screens application (without any particular interface requirements) it would be much faster to create it using Asp.net web forms.

Robert Koritnik