Cool trick with Visual Studio Templates

I’ve recently been playing around with creating a Visual Studio template to create a default set of classes and interfaces for a Model View Controller. Having never done anything with Visual studio templates before (other than use them), I think it turned out quite well. That being said, there was one thing that took a bit of a workaround.

When creating a Model View Controller (or MVC for short), I wanted the following classes/interfaces:

I[Something]View
I[Something]Controller
I[Something]Service
[Something]View (in this case, a Windows form)
[Something]Controller
[Something]Service

… where [something] is the name of the actual MVC you want to create.

The issue that I ran into was in the actual template code. I was using the $safeitemname$ template parameter where I needed it. Unfortunately this didn’t quite work well in the Controller, as the value of the $safeitemname$ being passed into the controller already was “SomethingController”, not just “something”. The workaround for this was to use the $fileinputname$ template parameter instead. In this case the value of $fileinputname$ was just “something”, not “SomethingController”. All the examples out there seem to just show $rootnamespace$ and $safeitemname$. Changing it to the $fileinputname$ worked like a charm.

So, moral of the story: Try using $fileinputname$, not just $safeitemname$.

I’m almost tempted to create one of these for GameScreens in my current code that takes care of game screen management, sort of like the XNA sample. It would make adding a new GameScreen an absolute snap.

Advertisements

One comment

  1. Dan

    My God, thank you! I’m using templates for the exact same purpose and could not for the life of me figure out why $safeitemname$ wasn’t working as expected.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s