Practical ASP.NET

Productivity Enhancements in ASP.NET MVC 3

Peter Vogel takes a look at Razor and the productivity gains in generating views that ASP.NET MVC 3 provides to developers.

As I discussed in earlier columns on ASP.NET MVC, I think that the chief issue organizations will be concerned with in adopting the tool will be developers' productivity compared to ASP.NET. While organizations may value the additional reliability that extending test driven development throughout the whole application will give them, they may balk at giving up the powerful UI controls (both in the box and from the third party market) that come with ASP.NET.

For instance, while I've seen several articles on how to support paging in an ASP.NET table, none of them are as quick as checking off the paging option in ASP.NET DataView. While adding a grid of data to an ASP.NET form can be done with a few mouse drags and some property settings, generating the necessary HTML and code in ASP.NET MVC is a much more time consuming process. This may not be a bad thing if you're paid by the hour (as I am) but most organizations will be concerned unless they are guaranteed this extra time will be offset by reduced time spent in other activities (e.g. in quality assurance and end user testing).

Obviously, if ASP.NET MVC can achieve productivity levels in the same ballpark as ASP.NET when it comes to generating an application's UI -- the View -- these concerns disappear. So it's not surprising that many of the new features in ASP.NET MVC 3 are aimed right at this issue.

ASP.NET MVC3 is certainly more flexible than previous versions. When you create a new project you have the ability to select the engines that will generate your Views and that will manage your testing. The testing engine defaults to Visual Studio Test (the only one delivered with Visual Studio). But the View engine choice offers you two alternatives: the original ASP.NET engine and Microsoft's new Razor engine. And it's Razor that's designed to reduce the time to create a rich View in ASP.NET MVC.

There's no doubt that Razor significantly reduces the number of keystrokes required compared to previous versions of ASP.NET MVC. To begin with, there's no need to type in script blocks anymore -- a single character (@) indicates the presence of code. If you have a variable called "world", it's the difference between typing this:

Hello, <%= world %>.

And typing this:

Hello, @world.

There is going to be the odd time when, if you have content that uses the @ sign (e.g. an e-mail address) that Razor may not do the right thing. Where you're mixing code and text, depending on your language, the politic insertion of some HTML tags may also be necessary to ensure that Razor doesn't misinterpret your intent. In this C# example, the span tags are present just to keep Razor from confusing code and text:

@if (status == "Rejected") {
<span>
Your order has been @status
</span>
}

In Visual Basic, I'd need to put the @ sign in front of my opening tag:

@If status = "Rejected" Then
@<span>
Your order has been @status
</span>
End if

But in Visual Basic, if I had no use for the span tags, I could also use the @: combination to ensure Razor doesn't get confused:

@If status = "Rejected" Then
@:Your order has been @status
End if

These are exactly the kind of finger movements that become second nature to developers working with a specific tool.

But what about those UI controls that ASP.NET developers take for granted? The WebGrid provides an example of what to expect in that area. This code creates a WebGrid, binds it to the data in the Model assigned to the view, specifies the sort column, and -- just like an ASP.NET GridView -- enables paging in a few keystrokes:

@Code
Dim grid = New WebGrid(source:=Model,
defaultSort:="CompanyName",
rowsPerPage:=4)
End code

This is a genuine control that accepts properties and does the right thing at runtime -- it's not a wizard generating unmodifiable batches of HTML. For instance, I can change the sort column by updating the relevant property:

grid.SortColumn = "CompanyId"

When I'm ready to display the grid I can call its GetHTML method.

Of course, the problem with this increased functionality is that I'm making my View "smarter" and, as a result, starting to defeat TDD. If I have logic in the view that is changing the sort order, I may have to resort to human-based testing to ensure that the page renders correctly under all circumstances.

There's a lot missing here that ASP.NET developers take for granted -- no server side events for instance. But, then, doing without those events is exactly what the MVC paradigm is all about.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube