Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store configuration settings for T4

I need to create some files using Visual Studio T4 (templating). What is the best way to import some configuration settings into multiple .tt files and have it accessible in the templating code portion.

like image 956
vanja. Avatar asked Mar 31 '09 03:03

vanja.


4 Answers

The simplest way to store configuration is by placing it as code in a shared .tt file, which you can include from multiple templates (i.e. if you need a connection string, make it a constant in a class feature block). However, if that is not an option and you need to access settings in .config file, I agree with John's suggestion.

like image 188
Oleg Sych Avatar answered Nov 05 '22 15:11

Oleg Sych


I'd do this the same way as for any normal piece of code. Using AppSettings, or a custom configuration section. If you do it a lot, you should create a Custom Directive Processor. This could emit a property granting you access to the custom configuration.

like image 45
John Saunders Avatar answered Nov 05 '22 16:11

John Saunders


I don't know a whole lot about T4, but it seems like while listening to Scott Hanselman's interview with Kathleen Dollard that she said the native Visual Studio T4 support does not allow multiple files. I seem to recall her mentioning an external harness for working with T4 allowing it though, and I know they mentioned Clarius Visual T4.

like image 39
Chris Shaffer Avatar answered Nov 05 '22 16:11

Chris Shaffer


Well, check out Oleg Sych's blog - he's the author of T4 Toolbox, and he shows how you can output to multiple files. Not sure if the T4Toolbox also includes support for configuration - worth a look!

His blog is great - and has lots of T4 stuff in it.

Marc

like image 1
marc_s Avatar answered Nov 05 '22 17:11

marc_s