Being on the southern tip of the world and add the slowest bandwidth anywhere (confirmed by Scott himself) we seldom get the greats like Scott Hanselman out to talk to us, let alone for free, and when he is on holiday… but that is what the S.A. Developer community got last night and what a night it was. He spoke last night on MVC and I made some notes I thought I would share.
The first concept is that
- ASP.NET > Web Forms
- ASP.NET > MVC
It’s an interesting way of thinking that ASP.NET is not Web Forms, since it is normally that we use those two interchangeable. ASP.NET is a framework for building web applications, if we use web forms on that framework is a choice not a requirement. Web forms in itself is a lie, it tries to make us believe that the web is stateful… so that we get the RAD/VB6 experience for development. The problem is that like the Matrix, the lie constrains us. MVC is the anti-RAD in a way, it opens up a lot more to the developer than Web Forms traditionally does. Knowing there is a lie, and knowing the truth can hurt you (the same way Neo knew there was no gravity, yet fell) and so MVC can hurt you. MVC is NOT a replacement for Web Forms, it is another way to solve a set of problems and some problems are better solved in web forms, and others in MVC (or ASP.NET Dynamic Data).
MVC is made up of models, viewers and controls and all of these are changeable. So the viewers uses web forms to render the HTML, but there are other options. One of those is NHaml which is a very different way to create HTML (can read about it here) and I thought that looked very interesting. Viewers should contain no logic, they should focus on rendering HTML only. What is very nice is that for the rendering side JQuery is bundled directly into MVC! It is also important to note that ascx files (ASP.NET controls) are not a
Separate from the viewers is the controls which uses the ASP.NET Routing feature of 3.5 SP 1 heavily. It is very elegant in it’s implementation and shows a forward thinking of convention over configuration. In other words it if you type in a URL like http://test/account/view, it will first check in the viewers\account folder for a view.aspx or ascx file and then in the viewers\shared folder for a view.aspx or ascx file. No config to say that this URL maps to this file. Controllers should contain the bulk of the logic but should not have any web concepts (i.e. don’t use the Request object). Obviously you could, but this breaks the important separation in the MVC design. What is nice, is that because the design of MVC is to help enforce separation, unit testing is amazingly simple and if you have the full versions of Visual Studio then it even builds unit tests for you! So while it may be very easy to put some code in a controller to check if a user is authenticated, you need to decide what level of tolerance for code smell you can handle.
Separate still from both of those is the models. Which is your data model for example LINQ to SQL. The purest form of MVC is you have your data model and then a model to talk to controllers so there is a separation in the models and everything is clean. How much benefit that has in real world is unfortunately not that much and because you have the power (you took the red pill) you can share the model from the models to the controllers.
After that Scott showed his latest project, called NerdDinner. Which is based on MVC and will be an open source solution through CodePlex one day. He showed a lot of the code and highlighted the good bits and the bad bits. This really highlighted what he was talking about and some of the problems you need to solve when working with MVC. After that was Q&A with Pizza.
Defiantly one of the best talks I have attended in a long time. He is on his way to Cape Town (taking a day out of his holiday to fly specially to CT) so if you are in CT, you must go and see him! Details can be found on S.A. Developer!