Blog Views


Tuesday, October 11, 2011

ASP.NET versus MVC Compare the Big Web Guns

Introduction


So I have seen a lot of ASP.NET Web Forms versus MVC blogs out there and I thought I'd put my two sense in as well. I used to be a big PHP and MySQL programmer, mainly out of lack of funds in other jobs and I have to say, when I first went to Web Forms, it was interesting to say the least. I can understand that the Web Forms concept came out of a need to allow Windows Forms programmers the ability to quickly adapt themselves into web world. Unfortunately, this came at many prices such as speed and browser compatibility. This post will try to explain both from a theoretical and practical side instead of learning each.

ASP.NET Web Forms


What is this Web Forms and where did it come from?

Web Forms has been around since January 2001 and was a replacement for ASP Classic by Microsoft programmers who needed a way to design web sites without much HTML or JavaScript knowledge. This new approach also allowed any back-end code (also know as code behind) to be written in .NET C# or VB. This and the dragging and dropping of controls helped in the transition from application to web platforms. Although a nice thought, there have been quite a few major pitfalls that have caused ASP.NET to be hard to work with.

Microsoft go wrong?? Never!!

ASP.NET has had some hard times through the years, but even now have some major issues that have kept it on the back burner when it comes to very serious Web Developers who need cross browser compatibility and fast performance. Where this comes out the most is when trying to do things such as pull client side code into the aspx file or styling your code. Master files were created to make template files that all other web application files within a project can use. Using Master files cause odd ID tags for controls to populate into the scrambled characters such as ct10034343. This makes it very difficult in JavaScript to access the DOM for things such as validation and CSS styling. Now you might be thinking, well just do the validation in the code behind. This will work, but it will cause performance lag. Depending on the calculation required to validate the form, there may be major lag times!

The other major thing that was introduced with ASP.NET is stateful applications inside a stateless protocol (HTTP). What does this all mean? It means that ASP.NET has the ability via ViewState to keep the state of the various .NET Controls as well as states of various strings and objects you may need to retain (such as paging or sorting). Although again a great idea, the ViewState has been used improperly by the majority of the developers whom use it. Most developers use that to store massive amounts of data or objects and this is sent to the Client EACH REFRESH! As you can obviously tell, this can bring performance down in a hurry...

What does this mean? No more Web Forms?

In my opinion. No. I think Web Forms is here to stay for a bit if not indefinite. This is for several reasons. One of those reasons is that it is still a fantastic tool for RAD applications (Rapid Application Development). When you need a simple form or newsletter tool, Web Forms is still great. This is true especially for the application programmer unfamiliar with web technologies such as HTML, CSS and the XHTML a stricter version of HTML.

Another reason is that Web Forms have been around for a long time and not everyone will just one day say "Lets migrate to MVC, screw Web Forms!". This will not happen for many reasons, some of which are resources and knowledge-base. For the application programmers who wrote the Web Forms, there will be a larger learning curve that will have to be addressed.


ASP.NET MVC


What is an MVC??

MVC (aka Model-View-Control) is a design model / architecture that was originally developed in the late 70's to create a separation of concerns. In it's simplest form, this means that all the different aspects of an application (Meaning Data Access layer - Calling Code - UI) are logically separated into different folders and classes according to the MVC architecture. The architecture has since been migrated into the .NET Framework and labeled ASP.NET MVC. It's ideas have taken the web world by storm as it is very hard to implement this architecture ( if even possible, I have never tried myself) in the Web Form model. MVC also allows for excellent Unit testing. i will not go into detail here, but thought it should be mentioned.

MVC is also a stateless as apposed to stateful like Web Forms. Stateless means that each time you refresh the page or POST (not POSTBACK), you loose the state of the page and anything that relates to it. There are things to help with this like ViewData which unlike ViewState lives on the server, not the client computer.

Model aka Data Access Layer aka prince (just kidding)

In this case the DAL is the Model (somewhat.. I'll explain better in other posts where we are discussing more technical aspects of MVC ). The Model is used to handle interfaces and repositories that hold all the "code behind" and data calls whether it be database, lists, server files etc. This separates data access layer logic and business logic ( validation for example) from UI logic via the controller.


View (User Interface or HTML)

Through the View MVC displays W3C compliant (assuming you code it that way) HTML formatted code. This makes it much more browser compliant (even IPad and IPhone). There is no hidden HTML characters in the ID's or extra gibberish that only IE understands. MVC also does a lot of object binding between the View, Model Controller making it very easy to use strongly data typed objects throughout your code. This allows you to easily utilize Visual Studio's awesome Intellisense.


Controllers (The BabbleFish)

The Calling Code or the translator (babble fish if you like Douglas Adams :) ) is known as the Controller. This handles the requests between the view and the model. This allows you to "almost" completely remove any Business or Data logic from the View logic. This allows you to not only focus on one section at a time, but also guarantee what your HTML and styles will output like and in turn better browser compatibility.


So what does it all mean?

So whats this all mean? Is MVC the god among men of web development? Will Web Forms fizzle out and never grace our presence ever again? Not likely. As i mentioned above, Web Forms are still widely used and is still an excellent RAD development web application framework. It is also a great alternative for windows application programmers who know nothing about web development and don't really wish to know the specifics in detail.

If on the other hand, you are looking to have a fast growing enriched site with web applications that provides clear HTML standard output, better performance / faster load times and separation of concerns, you may want to take a closer look at MVC. Its core is still .NET, so a lot of the code is the same which is great, but there are some newer syntax in the MVC 2 and 3 releases that you will need to learn and understand, but if you develop in .NET currently it should be too bad. The bigger learning curve to overcome will be understanding how things like POST and Refreshing works.

I hope this has been informative and you view my other posts.

No comments:

Post a Comment