June 2008 Blog Posts
…and the feeling’s right.
If I can muster up the time and motivation, I like to try and post lighter more humorous fare on Fridays such as my recent RAS Syndrome post.
On this beautiful Friday day (is that redundant too?), I started off fresh out of ideas (and motivation) so I wasn’t planning on writing a blog post at all, until this was just handed to me.
Which Software Blogger Do Girls Like Better?
In this post, Nick Berardi takes a quick look around at the demographic information collected by Google’s new ad planner.
Consider for a moment, what this data represents. It is a huge database of information on surfing habits of perhaps nearly the entire Internet, collected via huge data centers consisting of unfathomable computing power (as well as power consumption). Not to mention the huge amount of data it represents and the incomprehensibly advanced statistical analysis algorithms that must be employed to properly sift this data.
Nick has all this power at his disposal to answer the pertinent question of the digital age.
Which geek blogger do chicks dig?
In the list, he compares bloggers such as Jeff Atwood (aka CodingHorror or is it CodingWhore, which might explain his results), Joel Spolsky (aka big red WTF guy), Scott Hanselman (aka Mr. “Writes About Hair”)… and then there’s me, the short end of the stick.
Now Jeff might have over 10 times the subscribers I have, Joel may be the most famous and respected of the bunch, and Scott may have the edge on me in terms of forefivehead, but guess who wins the only metric that matters?
That’s right, me suckas!
Eat that, CodingHorror!
Map Reduce FogCreek to FogWeak, Joel Spolsky!
Hanselminute is a minute too late, Scott Hanselman!
Of course, at this point is when my wife pinches her fingers together and holds them up to my temple and makes a loud hissing sound of air rushing out. After all, compared to the traffic level of these guys, my traffic is small enough that her periodic visits to my site would make a dent in the percentages.
And as my coworker Eilon points out, I’m celebrating what is effectively a made up score. “Hey everyone, I created this chart with this made up score, for which I have the highest one. I rock!” My retort is that his fascination with X-Box Achievement points is no different.
I guess I won’t quit my day job and become a male exotic dancer then. Carry on everyone.
Technorati Tags:
humor,
Friday,
Ego
When you create a new ASP.NET MVC project using our default templates, one of the things you might notice is that there is a web.config file within the Views directory. This file is there specifically to block direct access to a view.
Let’s look at the relevant sections.
For IIS 6 (and Cassini)
<add path="*.aspx" verb="*"
type="System.Web.HttpNotFoundHandler"/>
For IIS 7
<add name="BlockViewHandler" path="*.aspx" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
What these sections do is block all access to any file with the .aspx extension within the Views directory (or subdirectories). Note that access is blocked by mapping these file extensions to the HttpNotFoundHandler, so that a 404 error is returned when trying to access a view directly.
This works great if you are using all the ASP.NET MVC defaults, but what if you’re using an alternative view engine such as NHaml or NVelocity? It turns out you can directly request the view and see the code.
You’ll want to make sure to add extra lines within this web.config file with the appropriate file extensions. For example, if I were using NVelocity, I might add the following (in bold).
For IIS 6 (and Cassini)
<add path="*.aspx" verb="*"
type="System.Web.HttpNotFoundHandler"/>
<add path="*.vm" verb="*"
type="System.Web.HttpNotFoundHandler"/>
For IIS 7
<add name="BlockViewHandler" path="*.aspx" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
<add name="BlockViewHandler" path="*.vm" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
You might be wondering why we don’t block access to all files within the Views directory. Excellent question! In part, because the Views directory is a common place to put other static content such as images, pdfs, audio files, etc… and we decided not to prevent that option.
However, we’re open to suggestions. We do have a content directory in the default template. We could consider requiring putting static files in there or in another directory other than Views. I’m not sure how people would feel if they couldn’t put assets intended to support Views within the Views folder.
UPDATE: Updated the registry settings per James Curran’s comment. Thanks James!
One of the most useful registry hacks I use on a regular basis is one Robert McLaws wrote, the “ASP.NET 2.0 Web Server Here” Shell Extension. This shell extension adds a right click menu on any folder that will start WebDev.WebServer.exe (aka Cassini) pointing to that directory.

I recently had to repave my work machine and I couldn’t find the .reg file I created that would recreate this shell extension. When I brought up Robert’s page, I noticed that the settings he has are out of date for Visual Studio 2008.
Here is the updated registry settings for VS2008 (note, edit the registry at your own risk and this only has the works on my machine seal of approval).
32 bit (x86)
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\VS2008 WebServer]
@="ASP.NET Web Server Here"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\VS2008 WebServer\command]
@="C:\\Program Files\\Common Files\\microsoft shared\\DevServer
\\9.0\\Webdev.WebServer.exe /port:8080 /path:\"%1\""
64 bit (x64)
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\VS2008 WebServer]
@="ASP.NET Web Server Here"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\VS2008 WebServer\command]
@="C:\\Program Files (x86)\\Common Files\\microsoft shared\\DevServer
\\9.0\\Webdev.WebServer.exe /port:8080 /path:\"%1\""
For convenience, here is a zip file containing the reg files. The x64 one I tested on my machine. The x86 one I did not. If you installed Visual Studio into a non-standard directory, you might have to change the path within the registry file.
I used to find the smart tag really annoying in Visual Studio because it is such a small target to hit with the mouse. Silly me, trying to expand a smart tag with a mouse.
When you highlight the tag with your mouse, it tells you that the keyboard combination of ALT + SHIFT + F10 will expand the menu.
What it doesn’t tell you is that there’s a two key combination that will also expand it. I learned this one from Karen Liu of the C# IDE team who also happens to have a blog post with a nice collection of other tips and tricks written last year.
Just hit the CTRL key plus the period key (CTRL + .)
I really like this approach when performing renames within Visual Studio because it’s totally contextual. Rather than starting with a rename refactoring, I can instead just start renaming something and the smart tag shows up and all I have to do is hit CTRL + . and then ENTER. Booyah!
When approaching an extensibility model, I often find cases in which I want to merely tweak the existing behavior of the default implementation and wish I didn’t have to create a whole new specific type to do so.

Instead of creating a specific type, I tend to write a decorator class that implements the interface and takes in both the default instance of that interface and a delegate (specified using a lambda of course).
Let’s look at a quick example to make all this abstract talk more concrete. I’m playing around with the NVelocity View Engine for ASP.NET MVC from the MvcContrib project. Rather than have each controller specify the view engine, I’d really like to specify this in just one place.
I could take the time to setup a DI container, but I’m feeling lazy and this is just a simple prototype application so I planned to just implement the IControllerFactory interface and have it set the view engine after creating the controller.
For those not intimately acquainted with ASP.NET MVC, you can override how the framework instantiates controllers by implementing the IControllerFactory interface.
However, at this point, I realized I was creating these one-off controller factories all the time. What I really wanted was some way to decorate the existing controller factory with a bit of extra logic. What I did was write an extension method that allowed me to do the following in my Global.asax.cs file.
ControllerBuilder.Current.Decorate(
(current, context, controllerName) =>
{
var controller = current.CreateController(context, controllerName)
as Controller;
if (controller != null) {
controller.ViewEngine = new NVelocityViewFactory();
}
return controller;
}
);
It’s a little funky looking, but what is happening here is that I am calling a new Decorate method and passing it a lambda. That lambda will be used to decorate or wrap the current controller factory and will get called when it is time to instantiate a Controller instance.
However, the lambda always receives the original controller factory so it can use it if needed. So in effect, the lambda wraps or decorates whatever the current controller factory happens to be.
In this case, all my lambda is doing is using the default controller to create the controller, and then it sets a property of that controller after the fact. However, if I wanted to, I could have had my lambda completely override creating the controller.
Here is the the code for the extension method to ControllerBuilder.
public static class ControllerFactoryExtensions {
public static void Decorate(this ControllerBuilder builder
,Func<IControllerFactory, RequestContext, string, IController> decorator) {
IControllerFactory current = builder.GetControllerFactory();
builder.SetControllerFactory(
new DelegatingControllerFactory(current, decorator));
}
}
As you can see, under the hood, I am actually replacing the current controller factory with a new one called DelegatingControllerFactory. But the implementation for this new factory is really simple. It simply calls a delegate that you supply. As far as the user of the Decorate method is concerned, this class doesn’t really exist.
internal class DelegatingControllerFactory : IControllerFactory {
Func<IControllerFactory, RequestContext, string, IController> _factory;
IControllerFactory _wrappedFactory;
public DelegatingControllerFactory(IControllerFactory current
, Func<IControllerFactory
, RequestContext, string, IController> factoryDelegate) {
if (factoryDelegate == null) {
throw new ArgumentNullException("factoryDelegate");
}
_wrappedFactory = current;
_factory = factoryDelegate;
}
IController IControllerFactory.CreateController(RequestContext context
, string controllerName) {
return _factory(_wrappedFactory, context, controllerName);
}
void IControllerFactory.DisposeController(IController controller) {
if (controller is IDisposable) {
((IDisposable)controller).Dispose();
}
}
}
With this in place, the next time I need to implement a one-off controller factory, I can simply decorate the current controller factory instead.
I’m starting to find myself using this pattern in a lot of places. The potential downside of this approach is that if someone else comes along who has to maintain it, they might find it difficult to understand if they’re not well acquainted with lambdas and delegates.
So it is a bit of a tradeoff between convenience for the code author and readability for the code reader.
Or, as my recent inbox tells me, you’re not afraid to ask. ;)
A coworker recently asked for some good resources on getting up to speed on the Model View Controller (MVC) pattern. Around the same time, I received another email talking about how people are confused around the difference between MVC and the Model View Presenter (MVP) pattern.
No better opportunity to apply the DRY principle by answering some of these questions with a blog post.
MVC
The first place to start digging into the MVC pattern is to look at the Wikipedia entry. That’ll get you a nice brief summary of the pattern along with a list of resources.
In MVC, the Model represents the information (the data) of the application and the business rules used to manipulate the data, the View corresponds to elements of the user interface such as text, checkbox items, and so forth, and the Controller manages details involving the communication to the model of user actions such as keystrokes and mouse movements.
If you’re really into it, you can go directly to the source and read the original papers from Trygve Reenskaug, the inventor of the pattern.
There you’ll learn from the original paper (pdf) that the initial name for the pattern was Thing-Model-View-Editor. The pattern was baked via a process of extracting, improving, and articulating existing command and control patterns used in the operation of Norwegian ship yards in order to streamline and improve operations.
MVP
That ought to get you going with the MVC pattern. Now onto Model View Presenter, which was a response to the inadequacies of the MVC pattern when applied to modern component based graphical user interfaces. In modern GUI systems, GUI components themselves handle user input such as mouse movements and clicks, rather than some central controller.
MVP was popularized by Taligent in this paper on the subject (pdf). More recently, Martin Fowler suggested retiring this pattern in favor of two variants: Supervising Controller and Passive View.
The Difference?
So what’s the diff between MVC and MVP? Using WinMerge, here it is!
Sorry, bad joke but I couldn’t resist.
The two patterns are similar in that they both are concerned with separating concerns and they both contain Models and Views. Many consider the MVP pattern to simply be a variant of the MVC pattern. The key difference is suggested by the problem that the MVP pattern sought to solve with the MVP pattern. Who handles the user input?
With MVC, it’s always the controller’s responsibility to handle mouse and keyboard events. With MVP, GUI components themselves initially handle the user’s input, but delegate to the interpretation of that input to the presenter. This has often been called “Twisting the Triad”, which refers to rotating the three elements of the MVC triangle and replacing the “C” with “P” in order to get MVP.
What About The Web?
If you were playing close attention, most of these articles focus on rich client applications. Applying these patterns to the web is a very different beast because of the stateless nature of the web.
ASP.NET WebForms, for example, attempts to emulate the rich client development paradigm via the use of ViewState. This is why many attempts to apply patterns to ASP.NET focus on the MVP pattern because the MVP pattern is more appropriate for a rich client application with GUI components. A while back I even tossed my Supervising Controller sample into the ring. The Patterns and Practices group at Microsoft ship an MVP Bundle for ASP.NET.
However, many web platforms embrace the stateless nature of the web and forego attempting to simulate a state-full rich client development environment. In such systems, a tweaked MVC pattern is more applicable.
This pattern has been adjusted for the Web for your application development enjoyment.
AFAIK, Struts, a Java web framework, is one of the first widely used web frameworks employing the MVC pattern, though most people now look at Rails as being the tipping point that really brought MVC to the web and popularized the MVC pattern for web applications.
ASP.NET MVC is a new (in development) alternative framework for ASP.NET developers that makes it easy for developers to follow the MVC pattern. This framework employs the MVC pattern rather than the MVP pattern because it does not attempt to emulate rich client development, and thus the MVC pattern is more appropriate.
More Reading
There’s a lot of good content out there that puts these patterns into historical and categorical perspective. Besides the links already mentioned in this paper, I recommend checking out…
Today’s Dilbert described one of the most challenging problems small consulting companies face. I’m sure Jon, Micah, and I remember experiencing this many times when trying to build and expand Veloc-IT (though Micah probably still gets the pleasure of dealing with this as he’s still chugging along).
Click the image to see the full size strip at Dilbert.com.
Yesterday, I wrote a post that contained the phrase “BFFs Forever” in the title. Commenters were quick to point out the redundancy of “Forever” in the title.
Nice - BTW, your title is redundant, so your title reads “IronRuby and ASP.NET Best Friends Forever Forever”
Thanks! But I had made the very same point in that post.
And for those that don’t know, BFF is an acronym for Best Friends Forever. Yes, the “Forever” is redundant in the blog title, but it’s just how people use it.
Case in point, how often do you hear the phrase “PIN Number” which expands to “Personal Identification Number Number”. I started looking for other examples of such redundant acronyms and sure enough, there’s a term for them and a wikipedia article describing them.
RAS syndrome stands for "Redundant Acronym Syndrome syndrome," and refers to the use of one of the words that make up an initialism or acronym as well as the abbreviation itself, thus in effect repeating that word. It is itself a humorous example of a redundant acronym.
And in case you didn’t notice, “RAS Syndrome” is itself humorously a RAS. The Wikipedia article points to other great examples.
- ATM machine
- HIV virus
- ISBN number
- CSS style sheets
- Please RSVP
- CNN Network
The article references another page with a collection of more redundant acronyms.
- LAN network
- DAT tape
- RF frequency
- UPC code
It’s not surprising that most of these come from the technology field as we tend to be very acronym happy and love our TLAs (Three Letter Acronyms).
So now it is my turn to coin a RAS. I am going to call it PCS syndrome which stands for “Premature Correction Syndrome syndrome.” This describes the condition in one is compelled to interject with a correction without having read through the rest of the contents, especially when the author offers an explanation for the seemingly erroneous item. ;)
I’m only teasing because PCS happens to me a lot. I find that the comments to many of my blog posts tend to focus on some irrelevant miniscule detail rather than the topic at hand. Which I’m not necessarily opposed to because that provides great fodder for future blog posts!
UPDATE: I just posted the working demo here.
I wish I could have been there, but I was celebrating my son’s first birthday (which gives me an opening to gratuitously post a picture of the kid here).

I’m talking about Tech-Ed 2008 Orlando of course where John Lam presented a demo of IronRuby running on top of ASP.NET MVC.
This demo builds on prototype work I’ve done with defining ASP.NET MVC routes and views using IronRuby.
The final missing piece was defining controllers using IronRuby. Working with members of John’s team, Levi (a dev on the ASP.NET MVC team) made the necessary adjustments to get a prototype IronRuby controller working with ASP.NET MVC.
Disclaimer: This is all a very rough prototype that we’ve been doing in our spare time for fun. We just wanted to prove this could work at all.
Unfortunately, we can’t release the demo yet because it relies on unreleased ASP.NET MVC code. When we deploy our next CodePlex interim release of ASP.NET MVC, the demo that John provided should actually work. :)
And for those that don’t know, BFF is an acronym for Best Friends Forever. Yes, the “Forever” is redundant in the blog title, but it’s just how people use it. As an aside, I found out recently that a good buddy of mine in L.A. is actually working on the set of a new show tentatively titled “Paris Hilton’s My New BFF”, where contestants compete to become Paris Hilton’s new Best Friend.
Technorati Tags:
aspnetmvc,
ironruby
My compadre Scott Hunter, another PM on the ASP.NET team, who happens to work on the Dynamic Data feature, recently put together an example of using ASP.NET MVC and Dynamic Data Web Forms together in the same application. Look for the link to MvcDynamicData.zip on this Code Gallery page.
I’ve been working on a post detailing a couple different ways to integrate WebForms and MVC, but life has been really busy for me lately. Hopefully I’ll get that to you soon.
In the meanwhile, you can probably learn all you need to know by looking at this example.
By the way, Scott was recently featured on .NET Rocks. We need to get a better picture of him taken. That whole Smoking Man image has got to go.