Scott Hanselman

Modifying the default code generation/scaffolding templates in ASP.NET MVC

March 28, '11 Comments [13] Posted in ASP.NET | ASP.NET MVC
Sponsored By
image

One of the things people like the most about my ASP.NE MVC 2 Ninja Black Belt Tips video, besides the Bill Cosby sweater, is the tip where I show how to modify the default CodeTemplates that are used in Code Generation in ASP.NET MVC (either version).

Eilon mentioned it on an internal mailing list this week so I thought it'd be worth surfacing again in case you haven't heard of this, Dear Reader.

Soon, we'll see even more powerful, flexible, and fun ways to customize your own Scaffolding in ASP.NET MVC 3, so keep an eye out, 'cause it's coming.

Bring the CodeTemplates local to your project

imageWhen you use the Visual Studio "tooling" (that means dialogs and stuff) to Add View or Add Controller, you're actually executing a T4 template and generating a little bit of code. Where does this start and how can you change it?

Try this. Create a new ASP.NET MVC project.

Now, go to:

C:\Program Files (or x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp (or Visual Basic)\Web\MVC (or 2) 3\CodeTemplates

See all those folders? Turns out everything under CodeTemplates are T4 templates that can be either modified in place or even better, can be brought local to your project. Local CodeTemplates will override the global ones.

Copy that folder (or just subfolders like AddController, or AddView) to your ASP.NET project. I do this by dragging from Explorer directly into the Visual Studio Solution Explorer as seen  here:

image

You might get some errors when you drop the folder. Ignore them and instead, select the Templates using Ctrl-Click, then right click and select Properties. (You can delete any templates you won't be using.)

image

Now, see where it says Custom Tool? Clear that string out completely. You're tell Visual Studio that you don't want these T4 Templates to be run during a build. They will only be called manually by the Tooling dialogs like Add View and Add Controller.

You change these templates, or in the case of Add View you can make your own. For example here I've made a new "My Awesome List.tt" as a copy of List.tt and it shows up in the Add View dialog.

image

Now I can not only totally customize what gets generated from the Add Controller and Add View dialogs, I can add custom templates to the drop down. Most importantly, these changes live in the CodeTemplates folder of my project and are carried along with my project in Source Control so others on my team or company can use them as well.

Have you customized yours? Enjoy.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Good Exception Management Rules of Thumb - Back to Basics Edition

March 23, '11 Comments [58] Posted in Back to Basics | Learning .NET
Sponsored By

Almost five years ago I posted this "Good Exception Management Rules of Thumb" and had some interesting and useful comments. As with all lists of rules of thumbs, they may no longer be valid. What do you think?

Cori Drew commented to me in an email that she often sees code like this in the wild (changed to protect the innocent 3rd parties). This kind of code inevitably shows up in a file called Utils.cs, which may be your first clue there's trouble.

public void HandleException(String strMessage)
{
//Log to trace file only if app settings are true
if (Properties.Settings.Default.TraceLogging == true)
{
try
{
TraceSource objTrace = new TraceSource("YadaYadaTraceSource");
objTrace.TraceEvent(TraceEventType.Information, 5, strMessage.ToUpper());
objTrace.Flush();
objTrace.Close();
}
catch
{
//do nothing if there was an error
}
}
throw new Exception(Environment.NewLine + strMessage);
}

What's wrong with this code, Dear Reader?

There's a number of things that we can learn from, so have at it in the comments in nice lists bulleted with asterisk, won't you? You can comment on lines, or the larger strategy or both. I'll update the post with a roll-up once you come up for breath.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

This Developer's Life 1.1.5 - Revolt

March 23, '11 Comments [1] Posted in Podcast
Sponsored By

15-revolution In this special episode of This Developer's Life we explore the Egyptian revolution. We talk to Remon Zakaria about his experience writing software for a day job and overthrowing the government at night. Big thanks to Ahmed Remy and Reem Ahmed for their help.

Download Here

We wish we could have used everyone's story - there were so many.

  • Remon Zakaria works at DashSoft, a software consultancy in downtown Cairo specializing in turnkey solutions for offshore companies.

You can download the MP3 here (42 minutes) and visit our site at http://thisdeveloperslife.com.

Please consider subscribing with iTunes, or Zune. If you enjoy it, take a moment and please do REVIEW our show on iTunes.

Or if you have a BitTorrent client and would like to help save us bandwidth money, as well as the bragging rights of downloading legal torrents via RSS, get our Torrent Feed at ClearBits.

The bandwidth and other costs for this week's show were picked up DevExpress and CodeRush! Visit them and thank them on Twitter.

DX_Slogan_350

Announcing our listener contest...This Developer's Life - Crowdsourced

Oh yes. We want to hear your stories. Record your best developer stories and send them to us and if we think they rock, we'll include them in the next episode of This Developer's Life.

What we need from you:

  • Your story. We don't want interviews, we want stories. Tell us about your passion, or something crazy that happened at work while solving some technical problem.
  • Keep your audio clean. Use a decent microphone or at least make sure you don't "overdrive" your microphone by talking to close or two loudly. Don't record while mowing the lawn and don't record in a giant echo chamber.
  • Be passionate. Talk to us like you're talking to a friend.
  • Don't worry about editing or music. Just share. We'll handle the Lady Gaga mashups.
  • Note we may move your audio around or change the order of stuff to make it more listenable or interesting or both.
  • Change the names of companies and people to protect the innocent (or guilty)
  • Know that by giving us your audio you're releasing it the Creative Commons and that we may or may not use it for a future show.

Send us a link to your audio file and what you're talking about and we'll do the rest. See you next time!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Microsoft "Video Kinect" Chat Review - Video Chat on the Big Screen, The Good and The Bad

March 22, '11 Comments [11] Posted in Gaming | Remote Work | Reviews
Sponsored By

Video Kinect Call I use Video Chat all the time. Like ALL the time. If you check out the Remote Work section of my blog you'll find dozens of posts about optimizing your video chat experience. Living in rural Oregon and wrking remotely for Microsoft, as well as my job as a community-focused open source individual means that I'm skyping or video calling people much of the week.

I have a Cisco Umi for work, and I use HiDef video cameras and Skype for talking to folks during the day. However, I'm always trying to find out the best way to talk to The Wife and Kids when I'm on the road. I wrote an article a while back called Skyping the Wife: Foolproof Video Conferencing with Your Family While on the Road where I setup auto-answer for Skype so my wife wouldn't have to do anything. However, Skype seems to have removed or hidden the auto-answer feature lately as they are constantly moving their features and options around. Plus, whenever I call home with Skype my wife has to drag out the laptop and with its camera's small field of view I usually just end up seeing the tops of the boy's heads. It's a hassle.

A few weeks ago I saw on Twitter that my Xbox 360 with Kinect supports Video Chat using an app called Video Kinect. Apparently this is already installed when you setup your Kinect so you probably have it already!

Video Kinect is effectively a Windows Live Messenger client. You can chat other Kinect/Xbox Live folks, but you can also chat or receive calls from anyone on your Live Messenger list of friends. One you log in to Messenger (be sure to save your password) you will see avatars for Xbox folks and Live Messenger icons (no faces, sadly) for Windows people.

Video Kinect Friends List

You can call from any of these screens, using your controller or your hands via Kinect.

Video Kinect contact list

First thing you should do, IMHO, is turn off the AutoZoom feature. It uses a Digital Zoom to artificially pan/zoom to the face of the person speaking. It's very disjointed, inconsistent but more importantly, it doesn't allow you to really appreciate the wide field of view that the Kinect camera gives you. It's massive and you can see the whole room...this reason alone is why I think Video Kinect will be THE way I talk to the family when travelling. Especially when the kids are running around.

Video Kinect options

The resolution of the Kinect is 640x480 which is pretty darn good. It looks fabulous from my laptop on the receiving side. See the pic below.

Video call with Video Connect and Live Messenger

The audio is excellent as well, surprisingly so. I can hear and see everything that's going on in the TV room which is fantastic with active kids.

The Good

The Kinect has a great webcam. Good resolution and easy to see.

It's on the big screen and using the big stereo. The kids, ahem, connect more with the large system than the laptop. Daddy's actual size on the flat screen.

The Bad

Either Video Kinect, the codec, or the camera sucks for quick quick action. I see only blurs when the kids are running around. Not sure if this is hardware or software, but it's pointed and reproducible. It's not a deal breaker, but it's clear that they've optimized for the "sit and chat" scenario, not the "watch the kids go insane" scenario. Surprisingly the latter is the 80% in my 80/20 world.

There's no option for FullScreen with PIP. Your local image is the same size as the remote one. At leas make this an option. It's weird.

Answering a call ON the Xbox is ridiculously hard. My wife was unable to do it and it takes too many button pushes. You have to press the center Guide button, then down to select the call notification from an Inbox that comes in like a game invite. "Friend wants to Video Kinect with you..." And all this must happen in (it seems) less than 30 seconds. I called and called and the wife just couldn't do it. This means that all our calls have to be originated by her. When she calls me, I just click Answer in Windows Live Messenger. She also found it too hard to log in, so I just keep it logged in at home as me and I log in as her when remote. I asked some friends and over half said they do the same. It's a common scenario that they (the Powers That Be) aren't optimizing for.

The Wish List

  • Fullscreen
  • Easier or auto-answer
  • Easier to launch Kinect
  • Better framerate
  • Skype support

Still, it's pretty sweet, and since I don't have Skype TV and my Umi is in my office, for me Video Kinect plus Live Messenger is the best solution for travelers calling kids back home today.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

NuGet Package of the Week #3 - PhoneyTools for Windows Phone 7

March 21, '11 Comments [0] Posted in NuGet | NuGetPOW | Silverlight | WinPhone
Sponsored By

image Have you implemented the NuGet Action Plan? Get on it, it'll take only 5 minutes: NuGet Action Plan - Upgrade to 1.1, Setup Automatic Updates, Get NuGet Package Explorer.

The Backstory: I was thinking since the NuGet .NET package management site is starting to fill up that I should start looking for gems (no pun intended) in there. You know, really useful stuff that folks might otherwise not find. I'll look for mostly open source projects, ones I think are really useful. I'll look at how they built their NuGet packages, if there's anything interesting about the way the designed the out of the box experience (and anything they could do to make it better) as well as what the package itself does.

PhoneyTools includes new controls, classes and techniques for Windows Phone 7

I've heard that since NuGet is bundled with ASP.NET MVC that some folks think it's just for Web Development. This weeks package is one for Windows Phone 7. You can make packages for pretty much whatever you like. Looks like there are already 26 libraries that are tagged with "phone" on the NuGet site.

These tools are by Shawn Wildermuth and include a bunch of useful classes and controls that make Windows Phone application creation easier.

image

I'll fire up Visual Studio with Windows Phone Tools that I downloaded from http://create.msdn.com. (See how clean my Visual Studio looks? Yours can be tidy as well.)

My Totally Nothing Windows Phone 7 App

You can see that I created a totally basic do-nothing Windows Phone 7 application and that the NuGet Package Manager Console is docked at the bottom.

I type:

PM> Install-Package PhoneyTools
'SilverlightToolkitWP (≥ 4.2011.2.1)' not installed. Attempting to retrieve dependency from source...
Done.
Successfully installed 'SilverlightToolkitWP 4.2011.2.1'.
Successfully installed 'PhoneyTools 0.5'.
Successfully added 'SilverlightToolkitWP 4.2011.2.1' to WindowsPhoneApplication2.
Successfully added 'PhoneyTools 0.5' to WindowsPhoneApplication2.

Looks like PhoneyTools brought in the SilverlightToolkitWP as a dependency. If I want to poke around, I can double click on the NuPkg (which is associated with the NuGet Package Explorer you can get here with ClickOnce) and see what's going on.

NuGet Package Explorer - PhoneyTools.0.5 (11)

Looks like he's got his project's assembly as well as an install.ps1 and uninstall.ps1 in the tools folder. What's in there?

References to assemblies are added by convention automatically

The install.ps1 and uninstall.ps1 run automatically when you, ahem, install and uninstall. He's got a check for the version of NuGet in there (that looks like it was copy/pasted from one of my packages) as well as a manual stop to add a reference. That extra line for adding References is not needed. I know Shawn so I'm sure he'll be fine with me using this as a teaching moment as it's not 100% obvious how this new stuff works.

param($installPath, $toolsPath, $package, $project) 

if ($host.Version.Major -eq 1 -and $host.Version.Minor -lt 1)
{
"NOTICE: This package only works with NuGet 1.1 or above. Please update your NuGet install at http://nuget.codeplex.com. Sorry, but you're now in a weird state. Please 'uninstall-package PhoneyTools' now."
}
else
{
$project.Object.References.Add("AgiliTrain.PhoneyTools"); #don't need this
}

It might seem intuitive to add the reference manually, but assemblies that are in the lib folder will actually be added and removed automatically as references just because they are in the lib folder! There's no need to do it manually.

In fact, you can have one package that includes versions of assemblies for many different Framework Versions. From the NuGet documentation on Supporting Multiple .NET Framework Versions and Profiles:

\lib
\MyAssembly.dll
\MyAssembly.Core.dll
\Net40
\MyAssembly.dll

In projects that target the .NET Framework 2.0 and the .NET Framework 3.5, NuGet copies both MyAssembly.dll and MyAssembly.Core.dll. But in projects that target the .NET Framework 4, only MyAssembly.dll from the Net40 folder will be copied. If you want MyAssembly.Core.dll to be installed in a project that targets the .NET Framework 4, you must include it in the Net40 folder

For Silverlight, he should do:

lib    
\sl4
\AgiliTrain.PhoneyTools.dll

And when NuGet 1.2, he can use \sl4-wp. That way someone can release a package that includes a different payload for Silverlight 4 vs. Silverlight on the Windows Phone 7.

Playing with PhoneyTools

Now that I've got the PhoneyTools installed, what can I do? According to the PhoneyTools CodePlex project, this is a project that contains several classes and controls for use with Windows Phone 7 applications. There are plans for:

And the word is that he's targeting Mix for the first release. This release is in beta. There's nice simple utility stuff like automatically fading "toast" style messages...

FadingMessage.Show("Doing something important...");

That do what you'd expect (you can make them look like whatever)...

image

To more sophisticated controls like an alternative SelectSwitch:

SelectSwitch

And a SimpleLongListSelector Control that handles a lot of boring work for you and makes creating a giant long list with categories easy so that this XAML:

<my:SimpleLongListSelector x:Name="longList">
<my:SimpleLongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageUrl}"
Height="75" />
<TextBlock Style="{StaticResource PhoneTextNormalStyle}"
Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</my:SimpleLongListSelector.ItemTemplate>
</my:SimpleLongListSelector>

Along with this LINQ query against a data source:

// For Complex Object and ItemTemplate
var qry = from g in games
orderby g.Genre, g.Name
group g by g.Genre into genre
select genre;

longList.SetGroupingItemsSource<Game>(qry.ToList());

Gives you a nice clean result, and you can click on the category headers to quickly jump to another category.

ItemTemplate

There's lots more, and remember that we noticed the Silverlight Toolkit for Windows Phone "'SilverlightToolkitWP" was a NuGet package as well? There's lot of info on that Toolkit at Tim Heuer's blog as it contains useful controls and classes as well. Between Phoney and the Silverlight Toolkit I think I'm ready to write my app.

Please continue to insist on NuGet packages for useful stuff. Put pressure on the groups you work with and convince them this is a useful thing.

Also, please join me at TechEd 2011 in Atlanta as I present DEV338 NuGet: Microsoft .NET Package Management for the Enterprise!

I'm glad folks are using NuGet for more than just Web Apps. I hope you do to, Dear Reader.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.