Friday, April 18, 2014

Code Generation in C#.net – T4Template in Visual Studio



Code Generation in C#.net – T4Template in Visual Studio
Problem Statement:
Need to create more number of *.cs file with [TestClass] and [TestMethod]
Mostly the code remains constant. Only few parameters changes.

Consider creating and testing [TestClass] and [TestMethod] to be written for all Continents, Countries, and States.

Solution:
Using a template to generate *.cs files with static code and only substituting the lookup values for all Continents, Countries, and States.

1.       Create  a class to hold configurable values

// Holds the single record with all values
    public partial class TestRecord
    {
        public string Regulationstate { get; set; }
        public string Authorityname { get; set; }

        public int rulecount { get; set; }

        public string Conti { get; set; }
        public string Country { get; set; }
        public string State { get; set; }

        private string mname;
        public String Methodname
        {
            get { return mname; }
            set { mname = String.Concat(Regulationstate, Authorityname, Conti, Country, State, Methodname, "()"); }

        }

        public TestRecord(string regulationstate, string authorityname, string conti, string country, string state)
        {
            Conti = conti;
            Country = country;
            State = state;
            Methodname = String.Concat(Conti, Country, State, Methodname, "()");
            Regulationstate = regulationstate;
            Authorityname = authorityname;
        }
    }


2. To hold the collection of TestRecord

//COllection of TestRecord List

    partial class TestRecordList : IEnumerable
    {

        public List<TestRecord> mdt { get; set; }

        public TestRecordList()
        {
            this.mdt = FillTestList().ToList<TestRecord>();
        }


        public IEnumerable<TestRecord> FillTestList()
        {
            List<TestRecord> testlist = new List<TestRecord>
            {
                new TestRecord("Asiacode","SoutEast","asia_test","india_test","tamilnadu_test"),
                new TestRecord("Asiacode","SoutEast","assd_ia","srilanfd_ka","codf_lombo"),
                new TestRecord("Asiacode","SoutEast","asia","pakistadf_n","Islamabdf_ad"),
                  new TestRecord("UKcode","London","asiad_d","pakisdf_tan","Islamafd_bad"),
                    new TestRecord("UKcode","Licester","asDD_ia","paDF_kistan","Islafdf_mabad"),
                      new TestRecord("USAcode","sss","asiDD_a","pakistan","Islamafd_bad"),
            };
            return testlist;
        }

        public IEnumerator GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }


2.  Create a new file RuntimeTempalte(*.tt) file using Add item to project in VS2013

3.  Kindly note the Name of the *.tt template is  TestRecordList (same as class) and marked as partial class

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.Xml" #>


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

 <# var ite = mdt.Select(x => x.Regulationstate).Distinct().ToList();#>
 

 <#foreach(var i in ite)
 {#>

 [TestClass]
 public class <#= i #> {

<# foreach(TestRecord  item in mdt)
{
if(item.Regulationstate ==i)
{#>

[TestMethod]
public void <#=item.Methodname#>
{
//select top 1000
//from Dataworks
//where
// statecode = "<#=item.Regulationstate#>";
//caption1= "<#=item.Conti#>";
//and caption2= "<#=item.Country#>";
//and caption3= "<#=item.State#>";

string caption1= "<#=item.Conti#>";
string caption2= "<#=item.Country#>";
string caption3= "<#=item.State#>";
}

<#}#>



<#}#>
}
<#}#>


4.  Write a small Console Application to Generate and export  to test.cs within the project.


    class Program
    {
        static void Main(string[] args)
        {

            TestRecordList ts = new TestRecordList();

            string content = ts.TransformText();

            string path = @"..\..\Op\test.cs";
            System.IO.File.WriteAllText(path, content);
        }
    }



5.       Output file generated is below



using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class Asiacode
{


    [TestMethod]
    public void asia_testindia_testtamilnadu_test()
    {
        //select top 1000
        //from Dataworks
        //where
        // statecode = "Asiacode";
        //caption1= "asia_test";
        //and caption2= "india_test";
        //and caption3= "tamilnadu_test";

        string caption1 = "asia_test";
        string caption2 = "india_test";
        string caption3 = "tamilnadu_test";
    }
    [TestMethod]
    public void asiasrilankacolombo()
    {
        //select top 1000
        //from Dataworks
        //where
        // statecode = "Asiacode";
        //caption1= "asia";
        //and caption2= "srilanka";
        //and caption3= "colombo";

        string caption1 = "asia";
        string caption2 = "srilanka";
        string caption3 = "colombo";
    }
    [TestMethod]
    public void asiapakistanIslamabad()
    {
        //select top 1000
        //from Dataworks
        //where
        // statecode = "Asiacode";
        //caption1= "asia";
        //and caption2= "pakistan";
        //and caption3= "Islamabad";

        string caption1 = "asia";
        string caption2 = "pakistan";
        string caption3 = "Islamabad";
    }
}
[TestClass]
public class UKcode
{

    [TestMethod]
    public void asiapakistanIslamabad()
    {
        //select top 1000
        //from Dataworks
        //where
        // statecode = "UKcode";
        //caption1= "asia";
        //and caption2= "pakistan";
        //and caption3= "Islamabad";

        string caption1 = "asia";
        string caption2 = "pakistan";
        string caption3 = "Islamabad";
    }
    [TestMethod]
    public void asiapakistanIslamabad()
    {
        //select top 1000
        //from Dataworks
        //where
        // statecode = "UKcode";
        //caption1= "asia";
        //and caption2= "pakistan";
        //and caption3= "Islamabad";

        string caption1 = "asia";
        string caption2 = "pakistan";
        string caption3 = "Islamabad";
    }


}

[TestClass]
public class USAcode
{
    [TestMethod]
    public void asiapakistanIslamabad()
    {
        //select top 1000
        //from Dataworks
        //where
        // statecode = "USAcode";
        //caption1= "asia";
        //and caption2= "pakistan";
        //and caption3= "Islamabad";

        string caption1 = "asia";
        string caption2 = "pakistan";
        string caption3 = "Islamabad";
    }
}