Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top