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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With