Wednesday, January 7, 2009

Asp.net MVC & jQuery the new standard

Wow its been a while since I'v posted something. Been busy at work and settling into the new house, holidays and every other crazy thing that happens around this time of the year.

But what I really want to talk about is the new ASP.NET MVC & jQuery and how this will be the standard way of building sites on the ASP.NET framework. I know its a bold disclaimer, but I fully believe it after using both on a couple of projects and coming away thinking.... why wasn't it always this fun to build sites in Dot Net!

To begin with the MVC part stands for Model-View-Controller. This is a very old concept dating back to 1979 which was described by Trygve Reenskaug, then working on Smalltalk at Xerox PARC(..I'm always surprised at the amount of ground breaking technology that went on at Xerox back than.).
In MVC, the model represents the information (the data) of the application; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages the communication of data and the business rules used to manipulate the data to and from the model.




You gain a lot by separating your website out like this. It makes it super easy to run Unit Tests against all 3 levels. You also lose the ViewState dead weight. No more limitation on just 1 form and the post back model goes right out the door. Now the down side is most server controls that use ViewState will not work. But MVC makes up for this with HtmlHelpers. Plus you get really nice looking REST like urls. Like so http://stackoverflow.com/questions/tagged/asp.net-mvc. Notice you don't see the ".aspx" ext or any extensions. You may think "who cares, so your url looks pretty". Yeah I thought the same thing also, but what makes this important is the url is self describing. It now has meaning and that meaning can be parsed and indexed by the all mighty GOOGLE search engine. You know what that means. Better search indexes, higher ranking and more eye balls finding your site!!

These nice urls are pointers to actions in your controller.
An action is basically a function waiting to be called by a url. And just like any function you can pass parameters to it and it can output data from it. This is huge because now not just your front end .aspx page have access to your logic, but so does anything that can communicate through http. This is what makes MVC such an easy fit for AJAX. You can call that action url right in Javascript and have it return some string information or better yet JSON objects.

This is where jQuery comes in handy.
jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.




jQuery makes javascript fun again. Like when you first discovered all the cool little floating boxs and color changing scripts you can do with it. You can even make it snow on your web site with Javascript. But than all the different browsers decided, they knew the best way to implement javascript. You had to add all that browser checking code mixed in with your logic and boiler plate code....ahhhhhhhhhhhh!!!. No wonder most people ran screaming and called javascript a toy language. jQuery takes care of all that and comes up with a genuis way of traversing the DOM using selectors.

jQuery selectors are a combination of CSS 1-3 & XPath. Essentially, the best parts from both of these query languages were taken, combined, and used to create the final jQuery expression language.


What really makes jQuery shine is the fact that its very extensible. There are 100s of plug-ins on the site. From form valadation plugin-ins, auto-complete controls with ajax to a full Grid control with built in search using local or remote data, column resizing and ording.

jQuery was so powerful that Microsoft decided to include it with its ASP.NET MVC Beta release.

Just imagine the possibilities. I know its a different way of thinking and building a site from a .NET view. But this is the way all major web platforms are built. Ruby on Rails, PHP, Java..etc. It makes sense for the web and makes development life a lot easier.

If you want some tutorials on some of the topics I talked about ScottGu's Blog has some really nice posts that get into the details of ASP.NET MVC.

jQuery's site has a lot of nice documentation on what jQuery is and how it works.

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.