Monthly Archives: February 2008

Cheat Sheet Needed for ASP.NET MVC

The cool kids over on the other side of the fence always make these really cool "cheat sheets" for whatever bit of tech kit they’re using and I fully expect someone with some good design skills to produce one for ASP.NET MVC.

You hear that Rob Conery?  Scott Hanselman?  How about you Phil Haack?  I fully expect Scott Guthrie, who is a Word among Bytes, to conscript some stylish hipster graphic designer to produce a masterful, stylish and yes, useful cheat sheet for the MVC masses at Mix ’08, an event I sadly won’t be attending because my company considers computers and those that make them work to be second class citizens.  I’m lucky if I get  to upgrade my IDE before the next one comes out much less attend an actual conference.  I’ve heard of conference swag but I’ve never actually received any of this mythical bounty.

Still, I desire, want and dare I say expect said cool cheat sheet.

Here are some examples for those that need some prompting and design ideas and to figure out just what in the hell I’m talking about:

CopySourceAsHtml for Visual Studio 2008

I ported the CopySourceAsHtml to work with Visual Studio 2008. There are some workarounds to get the existing addin to work with Visual Studio 2008 but they all assume you have 2005 installed, which I don’t. I recompiled from source, built against .NET 3.0, removed some crufty code thanks to FxCop and removed a call into olepro32.dll that wasn’t needed. I’ll provide the updated source if anyone cares, this is just a quick posting as I’m flying out the door :)

To Install

  1. Download the CopySourceAsHtml AddIn zip.
  2. Unzip to My DocumentsVisual Studio 2008Addins, if the Addins folder doesn’t exist just create it.
  3. Restart Visual Studio (you did close it first right?)
  4. Right-click some selected code and you’ll have a new Copy As HTML… menu item, that’s the gold, click it.
  5. Check the boxes in the dialog that comes up to your delight and then paste into your blog software or forum post.
  6. IMPORTANT: This Addin generates HTML code, so remember to switch into the HTML view in your blog software or forum edit box.
  7. Bob’s yer uncle

Sample

This is how a chuck of code looks when pasted (you’ll notice it uses your exact color and font settings):

[Test]
public void Select_ColumnList_Specified()
{
   SqlQuery qry = new Select("productid", "productname").From(Northwind.Product.Schema);
   ANSISqlGenerator gen = new ANSISqlGenerator(qry);
 
   string selectList = gen.GenerateCommandLine();
 
   Assert.IsTrue(selectList == "SELECT [dbo].[Products].[ProductID], [dbo].[Products].[ProductName]rn");
}

The Source

UPDATE: I’ve finally zipped up the source and uploaded it by request. Remember, I’m not the original author so all the good credit goes to someone else, I’m just the monkey that made it work on Visual Studio 2008. Here it is for your enjoyment:

CopySourceAsHtml-2.1.0-Source.zip

Mp3tag 2.40 Released

Anyone that’s using my Zune Marketplace Mp3tag source will probably want to upgrade to the latest version of Mp3tag, which as of today is 2.40!  I’ve been using the beta version for awhile and the tagging dialog is much better than in the original 2.39 release, easier to tag those albums where you only have a few of the tracks.

Get it from the official download site.

In a related note I’ve been using Mp3tag for about 2 years and I finally pulled the trigger and donated some dosh to this excellent free program.  Thanks to Florian Heidenreich for continuing to create such a great product!

What ASP.NET MVC Can Learn About REST from Rails

I'm starting to see more ASP.NET MVC samples and questions come out and I'm realizing that a large portion of the ASP.NET crowd doesn't even realize that a huge reason for the MVC movement is because of the Ruby on Rails framework. A lot of new .NET MVC developers are struggling with architectural questions that have already been debated and answered in the Rails community, which makes Rails a great resource for when you're first starting out or you're curious how to handle certain situations, like nested resources or how to structure your controllers.

Speaking of controllers one great thing from Rails that I hope more MVC developers embrace is REST. Instead of repeating everything just watch David Heinemeier Hansson's keynote speech from RailsConf back in 2006. Sure, it's almost two years but for ASP.NET developers it may as well be yesterday. I'd suggest starting from the second part since the first segment is just normal conference ra-ra-ra.

Check it out here (don't forget to download the slides that he refers to here).

Note

He talks about using a semi-colon in the URL to denote an aspect/action of a controller, like this:

/people/1;edit

Well, you can ignore that and just assume he *really* meant to say:

/people/1/edit

They dropped that semi-colon silliness in Rails 2.0 and it feels much cleaner.

Nested Resources In ASP.NET MVC

Often you'll need to represent some hierarchical or parent-child relationship in your application and one thing you'll struggle with is how to cleanly mesh both the parent and child controllers yet keep them nice and RESTful. The secret is in good routing.

The problem

A popular example is tickets belonging to events (event as in Burning Man, not OnClick) and you want to get all the tickets for a certain event, as well as be able to work with just tickets or events. You want nice and pretty urls as well, so you're hoping for something like this:

/events/1/tickets all tickets for event 1
/events/1/tickets/new add a new ticket for event 1
/tickets/list all tickets for all events

The Messy Way

My first idea was to add a Tickets action to my Events controller so I could call EventsController.Tickets(int eventId) but that didn't really help when I wanted to view all the tickets for all the events. Plus it broke the whole REST idea and that's bad for maintainability.

My second idea was a butt-ugly url along the lines of /tickets/list?event_id=1 but that just kicks the whole MVC, SEO-friendly url philosophy in the nuts. Repeatedly.

The Routes Way

A Big Thanks goes to Adam Wiggins whose post about nested resources finally set off the lightbulb in my brain. Instead of trying to make my controllers do all the work why not take advantage of the actual mechanism that's there to handle these sorts of things and put it to use. That would be the routing mechanism that makes all your urls pretty and dictates which controller does what. Here is the way to keep your urls pretty and to have both a separate Events and Tickets controller yet still maintain the cool parent/child relationship:

[code=csharp]RouteTable.Routes.Add(new Route { Url = "events/[eventId]/tickets/[action]/[id]", Defaults = new { controller = "Tickets", action = "List", id = (string)null }, RouteHandler = typeof(MvcRouteHandler) });[/code]

(yes, I know, my code formatting sucks, I'll update it this weekend)

Once I discovered this I smacked myself on the forehead for not realizing just how simple this whole thing was.

Microsoft Shareholder Value

I just saw a quote from Joe Rosenberg via the MSFTextrememakeover that included one of the most asinine and scary things I’ve ever heard about Microsoft:

Rosenberg said. “The company has lost sight of its principal focus, which is to produce value for shareholders.”

What’s scary is that this is from a “Chief Equity Strategist” yet after this quote I wouldn’t trust anything this guy has to say about money or investing because if there is one common theme among the most successful companies and individuals it’s that their primary focus is to do what they love and to be the best at doing it. Once you start chasing money for moneys sake the game is over and you’re done ever making any type of substantial financial gains. Rosenberg should know this and if he doesn’t perhaps he needs to pick up a BusinessWeek and read this article, “The Secret Behind Trump’s Success“.

So if you ever find yourself starting a venture simply because you think it’ll net you millions or because you’re secretly hoping you’ll be bought out then stop, take a breath and get a grip on reality. If you really want to make money find your passion and be the best at it, do everything you can with it, surround yourself with others who share the passion and push that passion, be uncompromising with it and don’t change your vision to fit into a committee or shareholder view.

Zune Marketplace as a Mp3tag Source

Given that the current (and past) Zune software lacks any decent metadata editing I’ve been using Mp3tag to adjust the various tags as well as grab album art.  One cool feature of Mp3tag is that you can look up album information from a variety of online sources, most notably Amazon.  From there you can grab track listings and album art to help flush out your metadata.

Only problem is that sometimes the Zune Marketplace files an album differently than Amazon which means it won’t show up correctly in your ZuneTag (see mine in the upper-right).  After poking around with Mp3tag’s extensible "web sources framework" and using Fiddler to watch the HTTP traffic to and from zune.net I cobbled together a Zune source that will pull down the exact album information as listed on the Marketplace as well as the album art.

There is a bonus as well, I believe zune.net just made their 800×800 album art available via the back-end service I’m using so now you can grab full 800×800 album art even on tracks you didn’t purchase directly from the Marketplace.

Just download Marketplace.zip and extract the single Marketplace.src into your %appdata%Mp3tagdatasources folder and you’ll be rocking! I’d also suggest you download the very latest beta of Mp3tag because the tag sources (what Marketplace plugs into) dialog is much easier to figure out for first timers, plus I always include the artist in the track listing and version 2.39n supports splitting this into the correct tags.

Download ZuneMarketplace.zip

If anyone actually uses this and needs help getting it up and running just drop a comment.

UPDATE #1:

Thanks to Scott for catching something I should have mentioned but completely forgot.  The Zune software can’t read ID3v2.4 tags, instead it can only handle ID3v2.3 so after you first install Mp3tag follow these steps:

  1. Go to Tools | Options
  2. Navigate to the ‘Tags’, then Mpeg options in the left-hand tree
  3. Set Write to ID3v2.3 UTF-16
  4. For those that like pretty pictures:

Mp3tag Options

UPDATE #2 (5/20/2012):

Updated the download link to point the latest source. A few search engines were still sending people here so I’m fixing up the link to the one that actually works .

Getting SubSonic Setup in Visual Studio

I’m a big fan of SubSonic, a ORM/DAL generator slash utility belt of goodness. Like any tool there is a little configuration and setup you need to do to get everything rolling and while Rob Conery has some great podcasts on doing just this sometimes you just need to remember that one little option vs. wanting to watch a 10 minute podcast again, regardless of how melodic and sweet are the dulcet tones of Mr. Conery’s voice.

1. Download and Install SubSonic.

Seems simple but hey, some people need to be told everything :)

2. Create SubSonic DAL as an external tool

Even though SubSonic supports Rails-style auto-gen of your DAL via build providers I like putting my model/DAL in a separate class library and sadly build providers don’t play well with class libraries.

Go to Tools | Externals Tools… click “Add” and make it look like this:

External Tools

  • That command is your path to sonic.exe
  • If you’re working with the MVC Toolkit (and why aren’t you?) then change “App_CodeGenerated” to “ModelsGenerated”

3. Create SubSonic DB as an external tool

SubSonic can also version and script your database, very useful for source control and distributing your application. Same as above but make it look like this:

External Tools (2)

4. Install SubSonic Schema

I dislike any warnings during a build and one you’ll get with SubSonic is it not knowing about the SubSonic config sections. I did a blog post on how to fix this awhile back but I’m repeating here for the lazy (like myself):

  1. Download SubSonicSchema.xsd (if you right-click to download make sure you save it with an xsd extension)
  2. Put it in C:Program FilesMicrosoft Visual Studio 8.0XmlSchemas (adjust accordingly for VS2008)
  3. Edit DotNetConfig.xsd in the same folder and add the following line:

    (I added it right underneath the <xs:schema> opening tag, seems to work. Also, if you’re using Vista you’ll need to edit DotNetConfig.xsd in an editor that was started with right-click, Admin, otherwise it’ll write a copy of the xsd into the Virutal Store and your changes will never take effect. Trust me, I learned this the hard way.)

  4. Close Visual Studio if it’s running, re-open, ta-da you now have IntelliSense as well as no more annoying “Could not find schema information for…” messages.

5. Install SubSonic code snippets

You add various sections into your web.config to wire up the magic and nothing is more boring than typing the same poop over and over again. I created some snippets that provide the various bits that you can download here (actually not quite yet since the bits are at work and my VPN is broken right now).

And Bob’s your Uncle, you have all the boring schtuff out of the way and now you’re ready to start genning ya some code. If you don’t actually know how to start genning your code then I’d suggest watching some of those screencasts I mentioned above.

Software I Use Everyday

I’m one of those people that enjoy rebuilding their machines every so often, either for performance reasons or just because I like to tidy things up.  I rarely keep software installs around since there are usually newer versions by the time I re-pave my machine so this is my "must have" list of software I reinstall every time, in one easy place for my future self to grab the downloads from.

General/Misc

  • CCleaner - My favorite registry/file cleaner.  I run it at least a few times a week.
  • Trillian - Pretty much the one and only IM client.  It supports all major IM networks and the 4.0 beta version eve handles GTalk and MySpace so there really is no need for anything else.
  • Virtual CloneDrive - It’s what I use to mount ISO’s on anything from XP to Vista, though I hear MagicDisc does a great job as well.
  • Windows Live Writer - It’s what I use to write my blog and it rocks, seriously easy.
  • Windows Live Photo Gallery - Great for tagging photos and includes built-in flickr.com uploading support.  Really polished looking.
  • FeedDemon - My RSS reader of choice, if you only use Google Reader you’re missing out on some really great features.  Awesome support for offline feeds and they just made it free!
  • ClipX - Great clipboard history manager.
  • Window Clippings - I’m always taking screenshots for various things, either blogs or product help or just to send off to a client to show them what they should be seeing.  Free, easy and awesome.
  • Password Agent - New - There are a slew of password vaults out there but for some reason I keep coming back to this one.  It doesn’t try to offer every feature under the sun, just the ones I need.

General Development

  • TortoiseSVN - Best subversion client I’ve used so far.
  • Intype - A TextMate-like clone text editor that I’ve started using more and more.
  • TextPad - Until Intype matures some more I still need a lot of the great features in this text editor.
  • Sysinternals Suite - When you need to know exactly what’s going on in your system these tools will help you explore the plumbing.
  • Ruby - A beautiful scripting language that I find myself using more and more for little tasks that I used to write applications for.
  • Virtual PC 2007 - Because sometimes you just need to test on a different or clean OS and this is even better than a lab full of machines.

.NET Development

  • mbUnit - I find myself preferring mbUnit to NUnit to unit testing.
  • NCover - Ever wonder just how much of your application is actually being tested?  Here you go.
  • TestDriven.NET - Best way to run mbUnit, NCover, NUnit, etc. from inside of Visual Studio.
  • FxCop - Because it’s always nice to know what the framework team would think of your code.

Delphi Development

  • GExperts - Best add-in for Delphi ever in my opinion.  Just the Ctrl-G makes it a must-have.
  • QC Plus - When you want to submit Delphi bugs or just browse the current issues the CodeGear provided QC client app sucks.  QC Plus pretty much blows it out of the water.

Web

  • Chrome - New - This used to say Firefox but honestly I haven’t installed Firefox during my last three machine reinstalls.
  • Fiddler - When you need to debug HTTP traffic this is the tool I reach for.
  • SmartFTP -  I used to use Filezilla but honestly I’m a sucker for a good looking UI.
  • Gmail Notifier - Notifications of new Gmail messages in your tray.  Simple and useful.

LAMP

I still do a lot of PHP work for a few clients so these are my "must have" tools for working with the LAMP stack.

  • PuTTY - For telnet/SSH access into a site’s shell
  • SQLyog - GUI for managing MySql databases
  • LAMP Virtual Appliance - A LAMP stack in a virtual machine, with images for VMware and Virtual PC/Virtual Server.  Great for testing

Media

  • Zune Software - Because I have a Zune and I love UI.
  • Mp3tag - My favorite metadata tag editor.
  • Winamp - Still the most powerful media player out there, plus it rips better MP3′s than the Zune software since it uses the LAME encoder.
  • EncodeHD – There are a dozen if not hundreds of tools used to re-encode video out there yet oddly enough they either look like a boom box from the 80’s, are super complicated or are expensive.  This is free, open-source, requires a single installer and frankly rocks.  Highly recommended.

Uh, now I realize why it takes me so long to reinstall my machine :)

UPDATE: Okay, so I’ve updated my list for 2010, look for  - New – for the items I’ve added since I last made this list.