Pushing the Boundaries of Automated Code Generation With T4 and Beyond

While this blog series on automation comes to an end, the opportunities to generate code continues. Here are some ideas for you on how to continue this journey on your own.

 

Automate smaller chunks of repetitive work

As soon as you get familiar with T4, the bar is getting lower to automate code. You know what you can do, and that knowledge helps you to make a judgement call when you see repetitive work.

For one application we needed an indicator next to each menu entry to show how many objects we have behind that type. While we only needed one class to show that, inside the class was a lot of repetitive code.

With our thorough knowledge of T4 we decided to automate that repetitive part, even when we only need one class. It was less effort for us to create a code generator than to go and copy and paste the code for the repetitive part.

We would not have done such a thing at the beginning of our automation journey. But with more experience the trade-off clearly tilted in the direction of automation. Hence, we automated that thing we only needed once because of all the repetition it hides inside.

 

The more standardised the work, the simpler the generator

I cannot stress this point enough: Make sure that the things you want to automate are standardised and that you have next to no exceptions to your rules. The less exceptions you have, the less if-statements you need in your code generators, the less maintenance work you have.

Even when it is annoying work, fixing the exceptions to your rules by hand may save you a lot of complexity in your generators. That little extra effort is worthwhile when you can save a few if-statements in the generator.

 

Structure the metadata if you need the special cases

Even when you do your best, there will be some cases that cannot follow the standardised way. For such situations try to create an abstraction that allows you a standardised way to handle the special cases.

Instead of an if-else tree in your code generator, try to create an Excel spreadsheet with those special cases. In the generator you then check if the field is in Excel and if not, go with the standardised way. That is a detour, but a detour is much simpler to update than an endless list of if-else statements.

 

Automate SQL Server Integration Services

At work we use SQL Server Integration Services (SSIS) to synchronise databases. While there is a graphical editor that allows us to manually draw the process and connect the fields, it is cumbersome and a lot of work. Instead of doing that manually, we created a solution based on Excel and the SQL Server Data Tools to automate the entire process.

We now can add a column to the tables in the source and the target database, add a row in Excel and recreate the SSIS packages in less time than it takes Visual Studio to open the graphical user interface.

This is a great example that the effort of an automation project not only can save you money, but it can reduce so much of the friction in a task that it no longer stopping you from adding newly gained knowledge directly to your code. It is a non-event to synchronise data; therefore, we just add new data that we find useful without thinking much about it.

 

Code generators in Roslyn

Roslyn not only allows us to interact with our solutions through code, we can use it to generate code as well. There are many good tutorials that explain the process in great details. However, whenever I try to do some code generation with Roslyn, I run into bugs or changes that break the examples I found.

Roslyn has much more changes than T4, and with those changes come breaking changes and bugs. Be aware that you need to invest a lot more time to keep up with Roslyn and go the extra mile to overcome bugs and shortcomings.

I stay with T4 for the time being, but I keep an open eye on Roslyn.

 

Parting Thoughts

I hope you enjoyed this series on code generation and automation in general. The T4 templates only change at the speed of a glacier, but thanks to its complete feature set, which makes it a feature and not a problem. Instead of constantly fixing changes in the base of your automation project, you get the time to focus on the business needs. It takes a while to get use to the T4 syntax, but when you get through that phase the templates are going to be an incredible help.

We currently add T4 code generators to a 9+ years old project. That is a lot of work, but we know the benefits it will bring us and know enough about T4 to take on that challenge. I strongly advice against doing such an endeavour with your first automation project. Try to get enough experience with a smaller, new project and only go for the big, old projects when you have some experience with T4. That will take a bit of extra time, but it will reduce a lot of friction and increases the odds of success massively.

I wish you all the best with your automation project. If you chose T4, use the examples in this blog to get a head start.

Leave a Comment

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