Posts Tagged dotnet

MVC 3 Technologies

www.asp.pngI had a brown bag session with my colleagues at work the other day to discuss the new technologies introduced with MVC 3. Here’s a summary of my session and what I feel is the most useful new features. This is from the perspective of a developer who works on a mission critical website that gets millions of hits per month.

Razor View Engine

The new view engine is a great evolution for MVC. Not only does Razor minimize the amount of code we need to type (devs R lzy!), but it also allows for a much cleaner code file compared to the old ASPX files. Of course there will be some effort needed to learn the new syntax, but it shouldn’t be too difficult. I think the most difficult part of learning the new syntax is figuring out what syntax Razor is “smart” enough to interpret on its own vs. what I will need to explicitly delimit as code. I guess the general rule is to assume Razor is “smart” enough to interpret what that I mean, and when build/test fails, adjust the code as needed. If you ever doubt how “smart” Razor is, you’ll get this nice error message at runtime:

razor export error.png

Another helpful feature is having the ability to declaratively create HTML helpers. I always thought it was awkward having to create/encapsulate HTML in C# code. Coming from an ASP.NET WebForms world with user controls encapsulating all the HTML code, having to code HTML in C# just seemed so wrong. The more natural @helper syntax just makes so much more sense! Unfortunately having to put the @helper code in App_Code instead of the original design of placing the code in Views\Helpers negates some of the benefits. We’ll have to review further to see if this could be leveraged on our website.

I feel the main disadvantage with the Razor syntax is figuring out how to integrate it into an existing project. Our website already has a substantial amount of code written using the ASPX syntax. Since rewriting the entire website at once is not feasible, we would have to do a hybrid solution with both ASPX code and Razor code. This will work for our team, but may cause headaches for other teams reading our code as well as getting new teammates up to speed with our codebase. Since there’s a pretty straight forward mapping between ASPX and Razor, I can easily see someone building a tool in the near future to automate this conversion!

Partial-Page Output Cache

Caching is key to creating a scalable and performant website. Our website uses a lot of partial views to encapsulate functionality so being able to output cache the partial views would be great! Currently we have our own helper class that encapsulates caching functionality. Having this functionality built into the framework would make the code a bit easier to manage. Too bad the documentation on this topic is lacking right now and I still haven’t been able to even test this feature out in test code. Once the documentation gets better, this is one feature I would like to revisit.

Unobtrusive Javascript

I get the idea of unobtrusive javascript: separate out the behavior from the content, but how does this impact a website outside of validation? Validation always seems to be the typical example, but I have yet to see how this works in other scenarios. We code our own jquery.validate on our website so using the built in unobtrusive validation isn’t very useful. Similarly with unobtrusive ajax calls, since we have our own framework for making ajax calls, this also isn’t very helpful. Seems like this is a good idea, but there needs to be more use cases before this becomes useful in an existing web application.

MVC 2 Upgrade to MVC 3

Wish there was a wizard to do this, but the manual steps outlined in the readme file actually isn’t too bad. Basically changing your version references and some GUIDs in your project and config files. My quick summary of the upgrade process from the release notes:

  1. Create empty MVC 3 project.
  2. Copy files form MVC3 project to MVC2 project: jquery.unobtrusive-ajax.js
    • /Scripts/jquery.unobtrusive-ajax.js
    • /Scripts/jquery.unobtrusive-ajax.min.js
    • /Scripts/jquery.validate.unobtrusive.js
    • /Scripts/jquery.validate.unobtrusive.min.js
    • /Views/Web.config
  3. If using Areas, copy /Views/web.config to Views folder of each Area
  4. In web.config, replace System.Web.Mvc,Version=2.0.0.0 with System.Web.Mvc,Version=3.0.0.0
  5. Delete existing System.Web.Mvc 2.0 reference and add 3.0 assemblies:
    • System.Web.Mvc
    • System.WebPages.dll (%ProgramFiles%\ Microsoft ASP.NET\ASP.NET MVC 3\Assemblies)
    • System.Web.Helpers.dll (%ProgramFiles%\ Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies)
  6. Edit the project file, replace {F85E285D-A4E0-4152-9332-AB1D724D3325} with {E53F8FEA-EAE0-44A6-8774-FFD645390401} in ProjectTypeGuids, save.
  7. In root web.config, add the necessary entries in the assemblies section and set bindingRedirect if needed. See release notes for exact code.

Other MVC3 Changes

The new ActionResult classes would have been useful a month ago! Other than being late, these are welcome new classes. Just as how there is a Response.RedirectPermanent() in ASP.NET 4.0, RedirectResult can be used to cause a redirect with 301 status code (permanent redirect) rather than the standard 302 redirect (temporary redirect).

JSON binding also falls under the category of “useful in the past, but not anymore”. I would imagine most teams out there using MVC already have their own framework to convert .NET objects into JSON code.

Software Releases

Finally with MVC 3, there is a bunch of new software releases. The NuGet Package Manager is interesting. If there is a way to setup a private repository server, then this feature would become infinitely more useful in an enterprise environment where multiple teams could easily share internal code. Since its currently based on a public repository (from what I read), then the useful of this is limited to just referencing public packages.

IIS Express and SQL Server CE is not very useful in a corporate environment since everyone would already have a copy of IIS7 from Windows and SQL Server.

Finally the release of the Web Farm Framework would make deployment so much simpler. Still looking into it but so far this looks to be very promising!

Thoughts and Resources

While looking for a cool image to include with this post, I realized that MVC doesn’t have a logo! Silverlight has one, ASP.NET has one (in this post). Since MVC is becoming more mainstream, Microsoft designers need to start designing a cool logo for MVC!

So that’s my quick summary and thoughts on the new features with MVC 3. Hopefully this will help out someone else out there too!

During my research, I reviewed these links to get up to speed.

MVC 3 Official Documentation
http://msdn.microsoft.com/en-us/library/gg416514(VS.98).aspx

MVC 3 Homepage
http://www.asp.net/mvc/mvc3

Scott Gu Posts
http://weblogs.asp.net/scottgu/archive/2011/01/13/announcing-release-of-asp-net-mvc-3-iis-express-sql-ce-4-web-farm-framework-orchard-webmatrix.aspx
http://weblogs.asp.net/scottgu/archive/2010/12/10/announcing-asp-net-mvc-3-release-candidate-2.aspx
http://weblogs.asp.net/scottgu/archive/2010/10/19/asp-net-mvc-3-new-model-directive-support-in-razor.aspx
http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

Phil Haak Posts
http://haacked.com/archive/2010/12/10/asp-net-mvc-3-release-candidate-2.aspx
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

,

No Comments