T4 Template Code generation
Recently in one of my project, I used T4 Template for quick code generation and it turns out to be a really helpful tool for accomplishing repetitive coding tasks, this inspires me to write this blog.
Many times in various parts of code development cycle we require similar piece of code with a little variance. It may be UI related code or business logic classes (Manager Classes) or Entity classes or could be stored procedures. I always felt that, this kind of code can be auto generated with the help of some tools instead manual efforts.
In my project I wanted to auto generate all the “Admin Web Site” pages which includes seed data editing as these may have similar kind of UI & functionality, like showing all the records into an Ajax grid and allowing them to edit on popup window etc.
There are number of third party tools available in the market today which are capable of generating reusable code. The only limitation I see is they have their predefined architecture and code generation strategies. Fitting auto generated code from different architecture to current application architecture is always a frustrating task at least for me.
Then another option I thought of is Code Dom, which comes along with .net framework itself especially for code generation purposes. When I explored this option in much detail, I realized that it requires huge learning curve which mainly includes writing code into “Intermediate Language” (IL). Of course it has many pros, like it can generate code for multiple languages from a single source template, but this is not what I want. I want very simple and easy to implement solution for generating similar kind of reusable code.
Now here comes Entity Framework code generation approach which uses T4 templates to generate all the source code files. When I dig more into it I found that it is pretty easier to implement this instead of “Code Dome” because it uses simple C# syntaxes for template writing. I was sure that T4 approach will serve the purpose so I decided to opt for it.
A brief Introduction about T4 Template
In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic. The generated file can be text of any kind, such as a Web page, or a resource file, or program source code in any language. You can find more detail in following link:
First I wrote one complete functional web page which I wanted to replicate for rest of web pages. Then I created two separate T4 templates, one for “ASPX designer file” and other for “Code behind file”. After this I copied previously developed Webpage into respective templates. Then I replaced hard coded entity names and column names with dynamic column names by using reflection. I also add logic to generate UI and data operations based on columns data types.
We are pretty much done here, now the only thing left which is to change entity name in the template and complete code will be automatically generated in minutes. Every time you need to generate code for different entities just replace the entity name in the template file.
Below is the snap of my ASPX file generation T4 template.
Below is the snap of my code behind file generation T4 template.