DotNetWise©

Saturday, March 12, 2011

T4 resx better generator for instead of ResXFileCodeGenerator

I have enhanced Ken's T4 template to be very similar with .NET 4.0 / VS2010 resx generator, including preserving the 'Custom Tool Namespace' property of the .resx file.

Also I have added the support to be able to generate enums.
There are two rules in order to generate enums:
1. The name should be EnumName_EnumValue1
2. You need to change the CustomToolName of the .resx file to anything that contains the word enum (you need to change it as you don't want to have two generated files)

The Value will be rendered via [Description(...)] attribute and the Comment will get into XMLDoc comment.
The nice thing about this enum integration is the fact that you can localize their description at runtime.

For example you could add this extension method and use it very simply:
var status = MyStatusEnum.AsString();
//Eating for en-US
//Mancand for ro-RO

AsString knows about both DescriptionAttribute and DisplayAttribute, but only DisplayAttribute is localizable.

You can download them both:

This is very powerful especially on ASP.NET MVC where you have various methods which return different values and depending on those you want to send a message to the View.
1. You won't need to declare any enum types anymore.
2. You won't hardcode your messages anymore.
3. You will return strong-type enum results
4. You will do a switch on the result of your method logic on the strong-type enums!
5. You can localize your messages later on :)
Happy codding!
Thanks for sharing Ken!

No comments:

Post a Comment

 
Blog powered up by Blogger