<?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>Dot Net Thoughts &#187; DSL</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/dsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +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>Code generation using T4 Templates</title>
		<link>http://www.dotnetthoughts.net/2009/07/31/code-generation-using-t4-templates/</link>
		<comments>http://www.dotnetthoughts.net/2009/07/31/code-generation-using-t4-templates/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 10:52:18 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Code Generation]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[T4]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=357</guid>
		<description><![CDATA[In the current project I am working we have to write lot of Business Entity classes, all of them got same structure of code, like lot of GET / SET code for Properties, and all these classes implemented by INotifyPropertyChanged interface. So we have to write the lot of same stuff repeatedly. Few days back [...]]]></description>
			<content:encoded><![CDATA[<p>In the current project I am working we have to write lot of Business Entity classes, all of them got same structure of code, like lot of GET / SET code for Properties, and all these classes implemented by INotifyPropertyChanged interface. So we have to write the lot of same stuff repeatedly. Few days back one of my local user group friend introduced me an impressive feature from MS Visual Studio called T4 (Text Templating Transformation Toolkit) templates. The T4 is a text transformation technology developed by Microsoft and used mostly for code generation by products like Microsoft DSL Tools. If you are using Visual Studio 2005, you need to install the SDK for T4 support, and in Visual Studio 2008 it comes pre-installed (Standard Edition and more).<br />
You can get more information about T4 from <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx" target="_blank">MSDN</a></p>
<p>Here is an example of a simple T4 template, which will create a Business Entity class with the INotifyPropertyChanged interface implementation.  The T4 template is nothing but simple Text files with some C# code.</p>
<ol>
<li>First you need to add a Text file, with the extension (.tt) </li>
<div id="attachment_358" class="wp-caption alignnone" style="width: 307px"><a href="http://anuraj.files.wordpress.com/2009/07/solution_explorer.jpg"><img src="http://anuraj.files.wordpress.com/2009/07/solution_explorer.jpg" alt="Solution Explorer" title="Solution Explorer" width="297" height="324" class="size-full wp-image-358" /></a><p class="wp-caption-text">Solution Explorer</p></div>
<li>Double click on the File, it will give you a warning like this. Click OK, it will generate a CS File (by default it is C#, you can modify to VB if you wish) under this template.</li>
<div id="attachment_359" class="wp-caption alignnone" style="width: 490px"><a href="http://anuraj.files.wordpress.com/2009/07/security_warning.jpg"><img src="http://anuraj.files.wordpress.com/2009/07/security_warning.jpg" alt="Security warning" title="Security warning" width="480" height="168" class="size-full wp-image-359" /></a><p class="wp-caption-text">Security warning</p></div>
<li>Lets add some code for the template.</li>
<pre class="brush: csharp;">
&lt; #@ template language=&quot;C#&quot; #&gt;
&lt; #@ output extension=&quot;cs&quot; #&gt;
</pre>
<p>These two directives indicates the Template Language in C# and output of the template is also in C#. You can also use output extension htm, if you want to generate HTML files.<br />
Now we are creating the basic class structure</p>
<pre class="brush: csharp;">
using System;
using System.ComponentModel;
public class &lt; #= this.className #&gt; : INotifyPropertyChanged
{
}
&lt; #+
string className = &quot;Sample&quot;;
#&gt;
</pre>
<p>These   tags called Statement Blocks, and are used to enclose control code. And the  tags called Class feature Blocks, which helps you to give features to the output class. The T4 Engine will use this take all the features from the template and Generate the class. And if you save the template again the security warning will come, say Yes, it will create the class file with basic structure, if you try to compile it will show some build error(s) like,missing the implementation of INotifyPropertyChanged interface, so let’s add the implementation.</p>
<pre class="brush: csharp;">
using System;
using System.ComponentModel;
public class &lt; #= this.className #&gt; : INotifyPropertyChanged
{
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
protected void onPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
&lt; #+
string className = &quot;Sample&quot;;
#&gt;
</pre>
<p>Now add the implementation of Properties(class features). For this first I created two-dimensional array, like this,</p>
<pre class="brush: csharp;">
string[,] properties = {{&quot;int&quot;,&quot;userId&quot;},{&quot;string&quot;,&quot;comments&quot;}};
</pre>
<p>in this I have two properties. Now add some code in the Template to generate the variables and Properies. Here is the full implementation.</p>
<pre class="brush: csharp;">
&lt; #@ template language=&quot;C#&quot; #&gt;
&lt; #@ output extension=&quot;cs&quot; #&gt;

using System;
using System.ComponentModel;

public class  : INotifyPropertyChanged
{
&amp;lt;#
for (int idx = 0; idx
private  _;
&amp;lt;#
}
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
for (int idx = 0; idx

public
{
get
{
return this._;
}
set
{
if(this._ != value)
{
this._ = value;
this.onPropertyChanged(&quot;&quot;);
}
}
}

#region INotifyPropertyChanged Implementation

public event PropertyChangedEventHandler PropertyChanged;
protected void onPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion
}
</pre>
<p>And here is the output from it.</p>
<pre class="brush: csharp;">
using System;
using System.ComponentModel;

public class Sample : INotifyPropertyChanged
{
private int _userId;
private string _comments;

public int Userid
{
get
{
return this._userId;
}
set
{
if (this._userId != value)
{
this._userId = value;
this.onPropertyChanged(&quot;Userid&quot;);
}
}
}

public string Comments
{
get
{
return this._comments;
}
set
{
if (this._comments != value)
{
this._comments = value;
this.onPropertyChanged(&quot;Comments&quot;);
}
}
}

#region INotifyPropertyChanged Implementation

public event PropertyChangedEventHandler PropertyChanged;
protected void onPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion
}
</pre>
<p>You can import (add reference) to assemblies in template like  using Import directive.<br />
You can get more information about T4 directives from <a href="http://msdn.microsoft.com/en-us/library/bb126509.aspx" target="_blank"> Statement Syntax in MSDN</a>.</p>
<p>Happy Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </ol>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/07/31/code-generation-using-t4-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
