Raving about Virtual PC 2007

I think I can say this in all sincerity and without hyperbole: Microsoft Virtual PC 2007 has changed my life. Maybe I’m a little late jumping on the virtualization bandwagon, but now that I have, I’m grinning with Kool-Aid stained lips. Sure, I’ve known that the technology has been out there for a while, but for some reason I just didn’t realize how cool it would be. For me it is kind of like TiVo in the fact that it sounds really neat before you check it out, but once you’ve used it you just can’t possibly live without it.

Before I get into my thoughts and share a few tips I’ve picked up over the past few weeks, I’ll give a quick overview of the technology for those who aren’t up to speed. The idea behind virtualization is that you can use your existing computer to work as two or more computers at the same time. It’s a lot like remote desktop, but instead of viewing another computer in a window, you’re viewing a virtual computer in a window. This virtual machine has a separate operating system and installed software, but can share devices like DVD ROM drives and internet connections with the host operating system.

As you can probably guess, virtualization lends itself particularly well to software testing, where it’s often important to have access to different operating systems and browsers. With a few clicks of a mouse you can load a virtual Windows XP desktop, test your web based software on IE6, and then go right back to your Vista/IE7 desktop. If you screw up the virtual operating system you can just replace it with a backed up copy.

So, now that I have you convinced that you need virtualization, I’ll walk you through the relatively simple process of getting everything set up and configured. First of all, dig around in your bios and see if there is a configuration option for turning on virtual machine optimizations. I have a relatively new Dell laptop, and for some reason the Intel virtualization optimizations were turned off by default.

Virtualization Bios

Then download and install a copy of Virtual PC from Microsoft’s website. Sure, there’s other (and arguably better) virtualization software out there, but this is free!

So, once the software is installed create a new virtual machine. During this process you’ll be asked to define a new virtual hard disk. This is the single file that is used to store all of the virtual files for the virtual machine. Be careful with the size you choose for the disk; it’s a little confusing and I messed up the first time through. The number you choose will allow the virtual disk to grow to that size. It will not take up the space unless you actually use that much inside the virtual machine. Unfortunately, you can’t increase this later, so be careful to not choose a value that is too low. If you have extra disk space, I’d recommend going with 8 gigabytes. I’d also recomend bumping the memory up to at least 256MB or 512MB if you have a lot of memory.

Virtual Disk

Now you need to install the operating system. Put the windows install CD in the drive and fire up your virtual machine. It’s kind of strange seeing the familiar windows install screens from a desktop window, and this was my first “oh, cool” moment. Another thing I tripped over during the install was that my mouse would become stuck in the virtual machine window. To send control back to the host desktop, just hit the right Alt button.

The next step is to install the virtual machine additions inside your newly installed virtual operating system. These can be accessed from the “Action” menu and allow you to drag and drop files from the host desktop, fixes the mouse locking annoyance, and allows you to share files. This would also be a good time to download the latest OS updates from windows update and install any other software you’re going to be using.

Now that you have your virtual machine image just the way you want it, we’ll back it up. The first step is to run a disk defragmentation inside of the virtual machine. Then run the precompactor utility inside of the virtual machine. You can access this by going to the CD menu, choosing Capture ISO Image, and browsing to C:\Program Files\Microsoft Virtual PC\Virtual Machine Additions\Virtual Disk Precompactor.iso. Finally, perform the actual Virtual Disk Compression on the host machine via the virtual disk wizard (accessible under File in the console). You now have a .vhd file that is ready for backup. That way, if you screw up your virtual machine you can just replace one file and you’re back on your feet.

Please let me know in the comments if my notes were helpful or if I should change anything.

Replacing my Strut with a Spring

My transition from Struts to Spring MVC

I came to Spider Strategies having only known Struts 1.2.x, and I was excited at the prospect of learning a new web framework, namely Spring MVC.

During my job interviews prior to being hired at Spider Strategies, I began to feel like a Struts apologist. Struts has, in some circles, been getting a bad rap. First released in June 2001, I admit its been showing its age. We, as an industry, have learned a lot about developing web applications since Struts’s inception, and no doubt any framework will begin to creak as technology continues its march. That said, I think Struts has held up reasonably well. The ability to integrate Struts and Spring is a testament to both of these frameworks’ extensibility. But, there are shortcomings in Struts that I was eager to leave behind.

These are my initial reactions. Keep in mind that I’m still very much a Spring MVC novice (though not Spring itself). Any mis-characterizations of Spring MVC are wholly due to my ignorance.

My transition so far is pretty easy. The difference between the two frameworks has not overwhelmed me. Both take the same action-oriented approach to web applications, a commonality for which I was most grateful. I have a feeling were I comparing Struts to a page-centric, JSF framework like Shale I would not be able to say the same.

That isn’t to say there aren’t many differences, but learning Spring MVC after knowing Struts was like learning to run with new footwear, as opposed to learning to run on my hands. Spring MVC allows more hooks into the request lifecycle, so I’m no longer limited to simply overriding an Action’s execute method. For example, I can override what is executed right before parameter binding occurs. I can also override “onBindAfterValidation” and perform a routine without messing with the “onSubmit” method. There are several such hooks, which is a refreshing change for me. Of course, having so many can lead to some confusion. I spent some time banging my head against the wall simply because I forgot the order in which these methods occur (thankfully Matt helped me out there — I do hope we’ll share a chuckle over that in the future :) ). But, Spring has always impressed me with the quality of its documentation, and its MVC component is no exception.

Any Struts veteran has been warned of the dangers of Action-chaining in Struts — that is advice well heeded. That such a pattern can so easily emerge is a deficiency of Struts. It’s a common design to have a “ViewUserAction”, and an “EditUserAction”, for example. If validation failed on the latter action, naturally you’d want to invoke the former action, and down the dark road of chaining. I can’t tell you how many times I’ve seen this, despite the problems it would cause. It also bothered semantically — what “action” was really occurring in “ViewUserAction”? Only after years with Struts I found somewhat satisfactory mechanisms that prevented chaining, namely ControllerSupport and TilesAction. Probably its my familiarity with these classes that allowed me to understand Spring’s controllers as well as I have. Underlying all the Spring request-handling classes seems to be this notion of being a request controller — in other words, an incoming request wasn’t necessarily something you wanted to take Action on. Rather, your job is to facilitate the request hand-off to the view. I dig that.

I’m still getting used to exposing my domain objects to the web tier now that those pesky ActionForms are gone. I’ve spent plenty of time creating fancy “transform(ActionForm)” methods in the past. I did find myself missing the ability to define an DynaActionForm in my XML when I needed a quick object to encapsulate request parameters. While you no longer need to derive form objects from a single class, you do need an object of some sort (when you need a form object at all, of course. Spring doesn’t assume that you need one for every request, unlike Struts). Being able to define validation via XML in Struts’ validation framework is something I miss (though its XML syntax was never something over which I had full mastery).

All in all, so far I’m quite comfortable in this new framework. I think Struts offered a great way to simplify MVC implementation, but didn’t offer you a lot of room to grow once you mastered the execute() method. Spring MVC, on the other hand, offers many more possibilities, and I look forward to exploring.

Theory vs Reality

Today we ran across an interesting problem in our software. It turned out we had accidentally hard-coded a bit of text in English instead of properly setting it up to be displayed in any language (this is called internationalization in geek-speak).

When we went in to fix this, we discovered the hard coding was done in our data access layer. According to theory, the data access layer has nothing do with internationalization, so we should move the internationalization logic to a different part of the application. In an attempt to conform to proper theory, it meant changing the code in our data access layer. This in turn required several changes to the service layer. The changes to the service layer in turn required even more changes to the user interface layer. As you can see, this disconnect between theory and reality quickly ballooned to the point where a very simple conceptual change would require many changes to the code.

Instead of ripping our software apart to conform to theory, we let reality take over on this one. The reality is our customers want working feature-rich software yesterday. They don’t want theoretically perfect software 10 years from now. So, we just added the internationalization logic directly to the data access layer so that we could cut the latest build of CMS 1.3, which expands CMS’ browser support to IE7 and Safari. We hope to make a general release of this software soon.

Test as Side-effect

I sometimes regard the term “unit test” as unfortunate. It’s certainly important that test cases actually certify your code, but in many ways I’ve come view that as a pleasant side-effect.

For developers unfamiliar with unit testing, the “test” aspect might be a tough sell. After all, they can actually see their code working once they’ve deployed their application. Indeed, prior to being hired at Spider Strategies, I found I had to disabuse potential employers of the notion that my unit testing experience was solely a function of QA.

For me, the less obvious benefits of unit testing are the more profound. Chief among them, it allows you to be more intimate with your code. One of the essays I enjoy in Getting Real is “Code Speaks”. Being metaphor-minded, I savor the conceit of listening to one’s code.

Allow me to stretch it to an absurd degree: if you’re waiting until you’ve deployed your application to listen to your code, it’s akin to having it shout at you from across the street. It’s much better to engage in a conversation with your code when it’s sitting nearby. In any good conversation each party can interrupt, correct, and acknowledge. Unit testing provides just that level of intimacy.

If you’re coding your test and find yourself having to create a dozen mocks and stubs, your code is telling you it is overly coupled. Once I found myself needing to mock the very class I was testing so that I could simulate a method invocation that took place in the tested method. That was my code telling me it was time to extract class. It has become accepted wisdom that tests should inform one’s design, and I’m glad for it. Code that is hard to test is generally accompanied by that foul odor with which we’re all familiar.

Do my test cases actually test? I suppose they do, in the end. But much like my IDE, I regard unit testing as a tool that allows easier software development.

DHTML Dialogs vs. Popups

DHTML and Popup Dialog Comparison

There are all kinds of situations in web applications were you need to collect information from the user, and our Corporate Management Suite is no exception. Like many web apps, we used to pop open a new, smaller browser window for this purpose. It certainly got the job done, but there were all kinds of problems.

The most annoying of these were popup blockers. Not only do the new browsers ship with blockers installed and activated by default, but add-in toolbars from Yahoo! and Google closed our new windows as fast as we could open them.

We also had problems with this popup window confusing novice computer users. It completely took them out of the flow of the application and they sometimes became lost if they temporarily switched to another program or accidentally clicked somewhere else.

Finally, and maybe most frustrating to me, the darn thing just looked ugly. It was always the same size, didn’t move with the main browser, and taunted us with its operating system specific minimize and close buttons.

After a fair amount of internal debate we decided ditch the popup window and move to a Dynamic HTML modal dialog. It ended up being a fantastic decision and we haven’t looked back. Gone are the days of having the popup and main windows get out of sync. Our dynamic modal dialog is actually part of the HTML page now, and blocks you from clicking anywhere else while it’s open. It also resizes automatically based on screen real estate and adds scrollbars when needed.

We’ve since incorporated the code into our webpage, so you can go to our products page and click on the automated product tour button on the right to see an example of it in action. The code is an extension of Dojo’s modal dialog and I’d be happy to clean it up and provide it for download if there’s a demand.