T4 (Text Template Transformation Toolkit) and partial class in C#
Edit
Working on a new project lately and laying my hand on news things! :)
One thing I had a better idea yesterday was T4 template comes naturally with Visual Studio. I have read about its existence before while I was exploring options for template engine for .Net MVC, I picked razor at the end so haven't put much time into it. But this time one of the colleague is using T4 to generate database query language. It was an eye opener for me. I start liking T4 as it automatically create the code behind the template. It's so magical to me that when I first read the code on my own, I keep looking for line of code that binding the model to the template (yes I am from the Razor world, ok later I figured out the "TransformText" method, which my colleague hid very well).
One more thing I learned was the support of partial class in C#. As the code behind for a T4 template is auto generated, you don't want to add your features in that piece of generated code because that will be update every time the template is being changed. So you write the custom code for the template, e.g. variable validation, in another partial class that VS will eventually putting the 2 partial classes together to compile to a single class. Here are the usage case stated from MSDN for partial class:
T4 (Text Template Transformation Toolkit) Code Generation
Partial Classes and Methods (C# Programming Guide)
One thing I had a better idea yesterday was T4 template comes naturally with Visual Studio. I have read about its existence before while I was exploring options for template engine for .Net MVC, I picked razor at the end so haven't put much time into it. But this time one of the colleague is using T4 to generate database query language. It was an eye opener for me. I start liking T4 as it automatically create the code behind the template. It's so magical to me that when I first read the code on my own, I keep looking for line of code that binding the model to the template (yes I am from the Razor world, ok later I figured out the "TransformText" method, which my colleague hid very well).
One more thing I learned was the support of partial class in C#. As the code behind for a T4 template is auto generated, you don't want to add your features in that piece of generated code because that will be update every time the template is being changed. So you write the custom code for the template, e.g. variable validation, in another partial class that VS will eventually putting the 2 partial classes together to compile to a single class. Here are the usage case stated from MSDN for partial class:
- When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
- When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
T4 (Text Template Transformation Toolkit) Code Generation
Partial Classes and Methods (C# Programming Guide)
T4 (Text Template Transformation Toolkit) and partial class in C#
Reviewed by DF
on
7:49:00 PM
Rating: