The advantage of t4 templates over class files

What is the advantage of t4 templates over class files in asp.net?

How we create a strongly typed class using t4 templates. We can do the same using C # class files in asp.net. So, what is the advantage of the t4 template over normal class files?

Someone please indicate that scenerio wer t4 can be implemented on top of class files.

Regards, Sujit

+6
source share
3 answers

I would start with this quote taken from the MSDN tutorial:

T4 text template is a mixture of text blocks and control logic that can generate a text file. Control logic is written as pieces of code in Visual C # or Visual Basic. The generated file can be any type of text, such as a web page or resource file, or the source code of a program in any language.

T4 templates are used to generate dynamic text, which can even be a class. An entity structure or DataSet constructor are good examples of creating design-time code, allowing the user to graphically drag objects from the database to the constructor, and then generate all classes to reflect your database tables.

Under the hood, a custom tool converts the generated XML development time to C # classes. The mechanism of this is similar to what the T4 template does.

I hope this review gives you a better idea of ​​how T4 templates can help.

0
source

You can write code that writes different code. There are things that you cannot reorganize into one place, or things that are easier to centralize through T4 (or any code generation).

There may be code where one central driver may need several files or many code in one file, which will be modified based on this.

Another way to use it well ... I used the T4 template to write a small C # object that generates a .sql file, a .xsl file, a .js and .cs . All from a single C # object based on a set of rules. Therefore, I generate several useful files from one C # template. I could create the .sql generator needed to create a table to store this type of ..xsl or .xml to determine the configuration values ​​associated with the element, the .js file to bring the strongly typed model to javascript and finally .cs for real model in c #.

0
source

Simply put, T4 templates can generate classes for you (and other files) based on some input, such as the Entity Framework model. This is very convenient in cases where you need to create many classes (for example, classes for all objects of the large Entity Framework model) with identical functions. Then the T4 template simply does the job and automatically updates the created classes if the input changes (you can run the "Custom Tool" on the input file to also run the T4 template).

The T4 template can (of course) create partial classes so you can add members (to other parts of the class you are creating). That way, you can add functionality that is not overwritten when the template recreates classes.

The T4 templates that generate the POCO classes (the default for EF in .NET 4.5) based on the EF model are a great example of the good use of T4 templates. Another example is the T4 template, which creates the base classes of the view model (for MVVM) for all entity classes of the EF model.

0
source

Source: https://habr.com/ru/post/944599/


All Articles