views:

55

answers:

2

i am planning to use T4 template to generate the config files. i have a main.tt file with basic settings. there are different .tt file for each environment which include the main.tt one thing which i want to achieve is how do i make sure that each environment specific .tt files override the main.tt variables. i need to do this since i want to make sure that all the values are overwritten in environment specific .tt files else T4 template will pick up the main.tt values. I want to avoid any missing environment values to be picked from main.tt How do we specify a mandatory override from main.tt?

<#@ template language=“C#” #> <#@ output extension= “.config” #> ” providerName=”System.Data.SqlClient” /> ” providerName=”System.Data.SqlClient” /> <#+ string NorthwindConnectionString = “Data Source=.;Initial Catalog=Northwind;Integrated Security=True”; string PubsConnectionString = “Data Source=.;Initial Catalog=Pubs;Integrated Security=True”;

>

how do i make sure NorthwindConnectionString and PubsConnectionString have specific values in all environment specific .tt files?

A: 

Here is an example of hot to generate config files with T4. There is no special feature in T4 to make these fields required. You can either check them in code of your main template or let it throw NullReferenceException, which is what I think this example does.

Oleg Sych
A: 

Anything inside a <# #> block is basically just .NET code, so how would you solve this problem without T4? Something like this would probably work fine:

<#
if (NorthwindConnectionString.Equals(
      "Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
    throw new CustomException("NorthwindConnectionString needs
        to be overriden in environment specific .tt file!");
}
#>
Michael Maddox