Using T4 to generate Configuration classes

Posted by Justin Hoffman on Geeks with Blogs See other posts from Geeks with Blogs or by Justin Hoffman
Published on Wed, 02 Mar 2011 13:56:59 GMT Indexed on 2011/03/03 7:25 UTC
Read the original article Hit count: 332

Filed under:

I wanted to try to use T4 to read a web.config and generate all of the appSettings and connectionStrings as properties of a class.  I elected in this template only to output appSettings and connectionStrings but you can see it would be easily adapted for app specific settings, bindings etc.  This allows for quick access to config values as well as removing the potential for typo's when accessing values from the ConfigurationManager. One caveat: a developer would need to remember to run the .tt file after adding an entry to the web.config.  However, one would quickly notice when trying to access the property from the generated class (it wouldn't be there).  Additionally, there are other options as noted here.

The first step was to create the .tt file.  Note that this is a basic example, it could be extended even further I'm sure.  In this example I just manually input the path to the web.config file.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly Name="System.Configuration" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ assembly name="System.Net" #>
<#@ assembly name="System" #>
<#@ import namespace="System.Configuration" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Net" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ import namespace="System.Xml.Linq" #>



using System;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;
using System.Linq;


namespace MyProject.Web
{
public partial class Configurator
{

<#
var xDocument = XDocument.Load(@"G:\MySolution\MyProject\Web.config");
var results = xDocument.Descendants("appSettings");
const string key = "key";
const string name = "name";
foreach (var xElement in results.Descendants())
{#>

public string <#= xElement.Attribute(key).Value#>{get {return ConfigurationManager.AppSettings[<#= string.Format("{0}{1}{2}","\"" , xElement.Attribute(key).Value, "\"")#>];}}

<#}#>

<#
var connectionStrings = xDocument.Descendants("connectionStrings");
foreach(var connString in connectionStrings.Descendants())
{#>
public string <#= connString.Attribute(name).Value#>{get {return ConfigurationManager.ConnectionStrings[<#= string.Format("{0}{1}{2}","\"" , connString.Attribute(name).Value, "\"")#>].ConnectionString;}}
<#} #>

}


}

The resulting .cs file:

using System;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;
using System.Linq;


namespace MyProject.Web
{
public partial class Configurator
{


public string ClientValidationEnabled{get {return ConfigurationManager.AppSettings["ClientValidationEnabled"];}}


public string UnobtrusiveJavaScriptEnabled{get {return ConfigurationManager.AppSettings["UnobtrusiveJavaScriptEnabled"];}}


public string ServiceUri{get {return ConfigurationManager.AppSettings["ServiceUri"];}}


public string TestConnection{get {return ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;}}
public string SecondTestConnection{get {return ConfigurationManager.ConnectionStrings["SecondTestConnection"].ConnectionString;}}

}


}

Next, I extended the partial class for easy access to the Configuration. However, you could just use the generated class file itself.

using System;
using System.Linq;
using System.Xml.Linq;

namespace MyProject.Web
{
public partial class Configurator
{
private static readonly Configurator Instance = new Configurator();

public static Configurator For { get { return Instance; } }

}
}

Finally, in my example, I used the Configurator class like so:

        [TestMethod]
public void Test_Web_Config()
{
var result = Configurator.For.ServiceUri;
Assert.AreEqual(result, "http://localhost:30237/Service1/");
}

 

© Geeks with Blogs or respective owner