a blog for those who code

Wednesday, 26 October 2016

Understanding Code Generation in Asp Net using Visual Studio

In this post we will be discussing about code generation in Asp.net. There are scenarios where you will come across large areas of code where the same code pattern is repeated over and over again. This is were code generation tools in Asp.net will come in handy where it will write program components automatically. There are number of commercial and free code generators are available in the market.

For example when you drag and drop any components from the toolbox in your asp pages, the codes of that component gets added automatically which is nothing but the code generation mechanism os Visual Studio. In this post we will be discussing about Visual Studio T4 for generating code in Asp.Net.

T4 system used by Visual Studio is template best where you start by creating a template file which contains portions of your code. Then the next thing is to run your code generation tool, which will pass the template to the generator. You can also pass external parameters which will combine the information from the templates and the parameters and saves the output to the disk.

Code Generation with T4


T4 templates are text files that specify the structure of the code or markup to produce. T4 comes with its own set of directives and blocks, which allow you to defined the boilerplate for code generation. To add a T4 template in your application you need to right-click on the project and chosse new item.


When you have added the template you can see there are some lines of code written as below

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>

which actually means that we are telling to this template that I want to use C Sharp as a language. Need a reference to the System.Core library and wanted to import System.Linq, System.text and System.Collections.Generic. Then at last it will show you how we want our output file to be. It will create a .txt file along with the .tt file with the same name as shown below.


Now to start with we will write a block which will dynamically create text for template as shown below

public class TextTemplate1 
{
  // Code Generated at <#= System.DateTime.Now.ToShortDateString() #>
}

Which will generate the code as shown below


There are different T4 blocks such as static text blocks, statement control blocks, expression control blocks and class feature control blocks.

Static text blocks simply represent what they means i.e. static text. They do not need any special syntax to identify them and they can appear anywhere in the template that you want to place them.

Statement control blocks contains C# language statements such as variable declarations, loops and conditional branching. They are represented by <# #> markup tags.

Expression control blocks are represented by <#= #> tags and it will output some value to the generated file.

Class feature control blocks which contains reusable code that can be called from other parts of the template. Ther ate represented by <#+ #> tags and they must appear at the end of the template file.

One more example would be to write a get and set property of any value as shown below

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<# var propertyName = "Prop"; #>

//----------------------------------
// <auto-generated>
// This code was generated by a tool

// </auto-generated>
//----------------------------------

public class TextTemplate1 
{
  // Code Generated at <#= System.DateTime.Now.ToShortDateString() #>
  public string Get<#= propertyName#>()
  {
return "";
  }
  public void Set<#= propertyName#>(int value)
  {
var result = value; 
  }
}

which will give the output as shown below


Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License