T4 to rescue and save you time

by Vahid 11. June 2010 05:56

till yesterday i had never used T4 for a real problem solving. i knew what it is but i did not know how great and easy it is.

in my current scenario i have a database table which contains options available for each property of 30 available properties of items to be added to the system. the table has almost 500 records.  in the item registration page i had to create a structure like this for each property:

<div id="div1" runat="server" class="row">
<asp:Label ID="SmartLabel13" runat="server" AssociatedControlID="" Text="Title of the property"></asp:Label>
<asp:DropDown ID="SmartDropDown1" runat="server">
<asp:ListItem Selected="True" Text="-----------" Value="">
<asp:ListItem Selected="True" Text="option 1" Value="option 1 value">
<asp:ListItem Selected="True" Text="option 2" Value="option 2 value">
.
.
.
.
<asp:ListItem Selected="True" Text="option n" Value="option n value">
</asp:ListItem>
</asp:DropDown>
</div>

creating 50 of these snippet and adding almost 12 list item for each of them along with setting the control names and property title and also AssociatedControlID of each would have taken me at least a day. i thought i could benefit from T4(Text Template Transformation Toolkit which is used for code generation) to save myself some time. so i started creating a T4 file to do this and vala… 15 minutes later i had it done. i have to say that, it was the first time that i actually was using T4. i cannot believe how easy it is to use it. the following few lines of code saved me at least a day by generating around 1000 lines of code:

<#@ template language="C#" debug="True" #>
<#@ output extension="txt" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #> 
<#@ import namespace="System.Data.SqlClient" #>
<#
string catName=string.Empty;
int i=50;
System.Data.SqlClient.SqlConnection cn=new  System.Data.SqlClient.SqlConnection("data source=.\\sqlexpress;integrated security=true;database=Irizzle");
SqlCommand cmd=new SqlCommand("select * from IRZ.Lookupvalues order by Category",cn);
cn.Open();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{    
if(catName==dr["Category"].ToString())
{#>
<asp:ListItem Text="<#= dr["Name"].ToString()#>" Value="<#= dr["Code"].ToString()#>" />
<#}
else
{
if(catName!=string.Empty){#>
</Irizzle:SmartDropDown>
</div>
<#}#>
<div id="div<#= dr["Category"].ToString()#>" runat="server" class="row">
<label for="drp<#= dr["Category"].ToString()#>"><#= dr["Title"].ToString()#></label>
<Irizzle:SmartDropDown ID="drp<#= dr["Category"].ToString()#>" runat="server">
<asp:ListItem Selected="True" Text="-----------" Value=""/>
<asp:ListItem Text="<#= dr["Name"].ToString()#>" Value="<#= dr["Code"].ToString()#>" />
<#}
catName=dr["Category"].ToString();
#>        
<#    i++;}
dr.Close();
cn.Close();
#>
</Irizzle:SmartDropDown>
</div>

i am sure T4 can save you and your companies hell lot of time. what i am wondering about is why not so many people talk about this great feature.

Tags: , ,

.Net | Technical

Comments

6/24/2010 2:28:47 PM #

online casino

Hey I just wanted to say that I really enjoyed reading your blog. You have good views, Keep up the good informative info Smile Just a quick question though. Are you making enough money from blogging?

online casino United States

7/1/2010 8:27:01 AM #

casino room reviews

You're a talented .net developer I see.You're spending so much time on programming and after that you publish all that codes in you blog.You're so generous!

casino room reviews

7/2/2010 7:08:43 AM #

Wire mesh

Happy to see your blog as it is just what I’ve looking for and excited to read all the posts.

Wire mesh People's Republic of China

7/2/2010 7:08:46 AM #

Wedding accessories wholesale

Thanks for this brilliant article. I am delighted after reading this. Thank you!

Wedding accessories wholesale People's Republic of China

7/2/2010 7:28:46 AM #

hemorrhoid treatment surgery

If your piles are additional persistent and agonizing  your medical professional may possibly advise minimally invasive health care technique  Probably the most typical sort of minimally invasive treatment method is rubber band ligation  exactly where your medical doctor  rubber bands  away the piles  cutting away their our blood circulation  This will bring about the piles to die and fall away  It normally will take about seven to ten times for this to happen

hemorrhoid treatment surgery United States

7/4/2010 11:05:28 PM #

Musician's Friend Promotional Code

This is great! It has been a fantastic read. I really learned a lot; and I could really use this information ! Thanks a lot for sharing.

Musician's Friend Promotional Code United States

7/8/2010 8:46:18 AM #

BuyCostumes Coupon

Great ideas here. You really gave me a few things to consider. Thanks a lot for sharing!

BuyCostumes Coupon United States

7/9/2010 1:35:19 PM #

coopervision

I wanted important data on this topic. The details was critical like me about to launch my personal portal. Many thanks for offering a missing link in my company.

coopervision Netherlands

7/12/2010 4:41:58 AM #

Guitar Center Source Code

Interesting post. I really enjoyed reading it. Thanks for sharing!

Guitar Center Source Code United States

7/13/2010 5:52:39 AM #

Home Depot Promotion Code

I really appreciate all this useful information! And I actually enjoy reading them, too. Thanks a lot for sharing.

Home Depot Promotion Code United States

7/14/2010 5:51:03 AM #

Gap Promo Code

Thanks for the heads up in this subject. Do you have any more resource, though? I'd like to learn more about this.

Gap Promo Code United States

7/15/2010 2:39:52 AM #

Sydney Wedding Photographer

Thank you. Your a superb net team lead. Over the last little while there have been a raft of posts about using T4 templates, I think most of this seemed to be sparked from the drive to extend and gain more control over the Linq-to-Sql classes. So while there are a few good T4 templates to convert DBML files into classes there have been a number of drawbacks.

Sydney Wedding Photographer United States

7/16/2010 5:32:14 AM #

Steve Madden discount code

Hey, thanks for posting this! I can really use this information. Keep posting!

Steve Madden discount code United States

7/16/2010 6:02:20 AM #

4 Man Tent

If you want to extend your conversation, you should definitely take a quick look at my most recent blog excerpt…

Shopping for a 4 man tent has never been easier. In times gone by, you would have needed to visit a local store and hope that they had the right product for you in stock. Now, you can take your pick from the mass of products on sale via the Internet, and available for home delivery. Browse as many sites as you can to get an overview of what is available, and then search for the best price on you model you eventually come up with. Just be sure to check the dimensions and the waterproofing before you finally order a 4 man tent.

There’s plenty more at the full posting as well as many supplemental materials to add value to your post above here…

http://www.4mantents.info

Thanks for the post!
Amity

4 Man Tent United States

7/17/2010 7:52:23 AM #

NBAStore Promotional Code

You sure have great ideas. Thanks for the useful information! Keep it up!

NBAStore Promotional Code United States

7/17/2010 10:55:23 PM #

Football Fanatics Promotional code

Hey, thanks for posting this! I can really use this information. Keep posting!

Football Fanatics Promotional code United States