Generate Code Into Separate Files With the T4.FileManager

When we generate code with T4, we end up with one single output file per template. That works if we only generate one HTML page or a single class. But when we want to generate multiple classes, the single output file is annoying. We never would let the developers write code that way; therefore, we should not let the automation get away with it either. Let us look at a solution to that problem.

 

The T4.FileManager

The single file output is a problem since the first release of T4. René Leupold created a solution for this problem back in 2010 with the T4.TemplateFileManager. Due to changes within Visual Studio, a complete rewrite was necessary, resulting in the new project T4.FileManager in 2020. I joined the project in December 2020 and support René since then with the development.

You can install the T4.FileManager as a NuGet package with this command:

After the installation, build your project so that Visual Studio copies the *.ttinclude files to the output directory. This will prevent the most common errors with missing *.ttinclude files and you can start directly with the code generation.

 

Generate code into separate files

We can reuse the code generator from last week and add these four marked lines to move the generated classes into their own files:

When we run this amended generator, we end up with two files inside the folder TestSubfolder:

Our two classes got their own file inside TestSubfolder.

Inside OrderDto.g.cs we have this code:

While ProductDto.g.cs looks like this:

We get the exact same code as last week, but each class has its own file.

 

More features

Generating code into separate files is the main reason for the T4.FileManager, but there are many more helpful features in this library. In the official documentation you get a description of each feature and an example in the form of a minimalistic T4 template as part of the corresponding acceptance tests.

The most helpful features from my point of view are these:

 

Naming convention and partial classes

With our newly gained ability to create classes into their own files, we face a new challenge: How can we separate the hand-written code from the files we generate? The simplest way to do that is to follow a naming convention. We use a .g in the file name to indicate to mark the files we generate. For C# classes, we would name the generated code file Customer.g.cs instead of the usual name of Customer.cs.

If we later want to add hand-written logic to the Customer class, we can create the Customer.cs file. That way we directly see what files belong together and the naming convention makes clear what file contains the generated code.

To make that possible we need to generate our C# classes as partial classes. This takes just one extra keyword in the class definition and gives us much more flexibility.

 

Next

Generating code into separate files may not look like a big thing. But trust me, it is a big difference for your automation project. That way we can push through the resistance in the development team and reuse the exact same demands to code quality as we use for hand-written code.

Next week we put our newly gained capability to work and generate classes based on tables in a database.

2 thoughts on “Generate Code Into Separate Files With the T4.FileManager”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.