<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Journey of a Software Developer &#187; T4</title>
	<atom:link href="http://www.tyronedavisjr.com/category/t4/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tyronedavisjr.com</link>
	<description>HTML &#124; CSS &#124; JavaScript &#124; ASP.NET &#124; PHP &#124; C# &#124; Java &#124; VB.NET &#124; MS SQL &#124; MySQL &#124; Flash</description>
	<lastBuildDate>Sun, 17 Apr 2011 03:51:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A little fun with T4 Templates</title>
		<link>http://www.tyronedavisjr.com/2009/12/15/a-little-fun-with-t4-templates/</link>
		<comments>http://www.tyronedavisjr.com/2009/12/15/a-little-fun-with-t4-templates/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 03:49:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[T4]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.tyronedavisjr.com/2009/12/15/a-little-fun-with-t4-templates/</guid>
		<description><![CDATA[So, if you haven’t heard yet about T4, it’s a built-in DSL tool that comes pre-packaged with Visual Studio starting with the 2005 version. Even though T4 can look a bit scary, it can be very useful if you need to generate code on the fly within Visual Studio. I will leave it up to]]></description>
			<content:encoded><![CDATA[<p>So, if you haven’t heard yet about <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx" target="_blank">T4</a>, it’s a built-in DSL tool that comes pre-packaged with Visual Studio starting with the 2005 version. Even though T4 can look a bit scary, it can be very useful if you need to generate code on the fly within Visual Studio. I will leave it up to <a href="http://www.hanselman.com/blog" target="_blank">Scott Hanselman</a> to fill you in on all the details since he has an excellent <a href="http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx" target="_blank">blog post</a> on what T4 is an where you can go for more information.</p>
<p>Last week I wanted to generate a object wrapper around the &lt;appSettings&gt; section in my web.config/app.config file and after doing the same thing over and over again by generating a public read-only property for each setting, I decided to use T4 to read the &lt;appSettings&gt; section and generate a class all the properties that would become the wrapper around the ConfigurationManager.AppSettings[“…”] calls to retrieve the settings. To my surprise, this was pretty easy to do. I did a C# and VB.Net version and here is the C# version of the T4 template.</p>
<pre>
<pre class="brush: vb">
&lt;#@ assembly name=&quot;System.Core&quot; #&gt;
&lt;#@ assembly name=&quot;System.Xml&quot; #&gt;
&lt;#@ assembly name=&quot;System.Xml.Linq&quot; #&gt;
&lt;#@ import namespace=&quot;System&quot; #&gt;
&lt;#@ import namespace=&quot;System.Text&quot; #&gt;
&lt;#@ import namespace=&quot;System.IO&quot; #&gt;
&lt;#@ import namespace=&quot;System.Linq&quot; #&gt;
&lt;#@ import namespace=&quot;System.Xml.Linq&quot; #&gt;
&lt;#@ import namespace=&quot;Microsoft.VisualBasic&quot; #&gt;
&lt;#@ template language=&quot;VBv3.5&quot; debug=&quot;True&quot; hostspecific=&quot;True&quot;  #&gt;
&lt;#@ output extension=&quot;.g.cs&quot; #&gt;
&lt;#
	Dim projectNamespace as String = &quot;SettingsGenerator&quot;
	Dim fileName as String = &quot;app.config&quot;
	Dim path as String = &quot;&quot;

	Try
		path = Host.ResolvePath(fileName)
	Catch
		Try
			If fileName.ToLower() = &quot;app.config&quot; then
				fileName = &quot;web.config&quot;
			Else If fileName.ToLower() = &quot;web.config&quot; then
				fileName = &quot;app.config&quot;
			End If
			path = Host.ResolvePath(fileName)
		Catch
			path = &quot;&lt;&lt; App.config or Web.config not found within the project &gt;&gt;&quot;
		End Try
	End Try
#&gt;
//------------------------------------------------------------------------------
// FileName = &lt;#= path #&gt;
// Generated at &lt;#= Now.ToLocaltime() #&gt;
//
// &lt;auto-generated&gt;
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
//
//	  NOTE: Please use the Add a Reference to System.Configuration assembly if
//	        you get compile errors with ConfigurationManager
// &lt;/auto-generated&gt;
//------------------------------------------------------------------------------

using System.Configuration;

public static class AppSettings {

&lt;#= RenderApplicationSettings(path) #&gt;
}

&lt;#+ 

	Public Shared Function RenderApplicationSettings(ByVal path as String) as String
		If Not File.Exists(path) Then Return &quot;&quot;

		Dim sb as New StringBuilder()

		Dim doc as XDocument = XDocument.Load(path)

		For Each result as XElement in doc...&lt;appSettings&gt;.&lt;add&gt;
			sb.Append(vbTab)
			sb.Append(&quot;public static string &quot; + result.@key + &quot;{&quot;)
			sb.AppendLine()
			sb.AppendLine(vbTab + vbTab + &quot;get {&quot;)
			sb.AppendFormat(&quot;{0}return ConfigurationManager.AppSettings[&quot;&quot;{1}&quot;&quot;];{2}&quot;, vbTab + vbTab + vbTab, result.@key, vbCrLf)
			sb.AppendLine(vbTab + vbTab + &quot;}&quot;)
			sb.Append(vbTab)
			sb.AppendLine(&quot;}&quot;)
			sb.AppendLine()
		Next

		Return sb.ToString()

	End Function
#&gt;
</pre>
</pre>
<p>Click the download images below to download the templates for both VB and C#. Until next time&#8230;</p>
</p>
<p>Happy Programming!</p>
</p>
<p><iframe title="Preview" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="width:98px;height:115px;padding:0;background-color:#fcfcfc;" src="http://cid-0ae385ab52b1ccf1.skydrive.live.com/embedicon.aspx/Public/AppSettingsCS.tt"></iframe></p>
<p><iframe title="Preview" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="width:98px;height:115px;padding:0;background-color:#fcfcfc;" src="http://cid-0ae385ab52b1ccf1.skydrive.live.com/embedicon.aspx/Public/AppSettingsVB.tt"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tyronedavisjr.com/2009/12/15/a-little-fun-with-t4-templates/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

