To generate or not to generate? Every developer and development team has their own opinion on design-time code generation. At Apriorit, we say generate but with the right tool.
Code generation at the design stage saves a lot of time on coding, helping you deliver a product faster while ensuring its high quality. However, to ensure quick transformation of your code into a low-level language with few to no errors, you should first choose a tool that suits your project’s specifics.
In this article, Apriorit experts overview the nine most popular automated code generation tools and template sets. This article will be useful for development leaders who are looking for the most suitable design-time code generation tools for their projects.
Why generate code at the design stage?
Source code generation is the process of transforming high-level code written by a human into a low-level language. In other words, this process generates source code based on the project’s description or model.
Code generation allows your developers to concentrate on a high level of abstraction during development. It also helps development teams save time when coding and protect their code from human errors that are inevitable even for the most skilled developers.
Generating code while configuring a design-time build allows for using generation results during compilation. The most popular tool for generating code is a compiler, and you’re free to use any compiler you find effective for your task.
For example, you can consider using the Roslyn compiler. At Apriorit, we’ve found that this compiler is a good option if you need to:
- Generate C# or VB.NET code
- Create a code generator
- Prioritize generation logic over the resulting code template
However, there are dedicated tools for converting your high-level code into low-level code. Let’s take a closer look at some of the different automatic code generation tools you can use.
9 popular templates and tools for design-time code generation
There’s a wide variety of design-time code generation software. Some is embedded in popular development environments like Microsoft Visual Studio, while other products are independent solutions.
When choosing a design-time code generation tool, pay attention to those that align with:
- Your chosen programming languages and frameworks
- The type of application you’re planning to develop
- The development environment you’re using for the project
- Your project budget
- And other criteria
Here are some of the most popular design-time code generation templates and tools:
All of these templates and auto code generation tools are useful when you need to create simple repetitive code like a client proxy for OData, or any text according to a template.
Let’s take a look at each of the template sets, tool types, and dedicated solutions mentioned above.
1. XLS Transformation templates
You might have already heard of the eXtensible Stylesheet Language (XSL), which is a styling language for XML documents. eXtensible Stylesheet Language Transformation (XSLT) is the part of this language that’s responsible for converting XML files to other formats. This solution for template creation is part of the W3C XLS standard. The key advantage of XSLT compared with similar tools is its flexibility.
XSLT is designed to convert the hierarchical structure of an XML document to HTML, PDF, text, source code, etc. The XSLT language is a powerful tool for manipulating data and information in a hierarchical structure.
XSLT is optimized for creating conversion rules. It consists of a set of rules marked with template tags. A rule consists of static text and a number of tags, reminiscent of the construction of logical languages: display value, cycle, condition, etc.
Unlike classical programming languages, XSLT describes transformation not as a set of actions but as a set of rules applicable to incoming XML nodes. Each rule contains a logic function (a predicate). You can determine if a function is relevant to a current node by calculating this function.
In XSLT, such functions are described with the XPath language. If a predicate is true, the rule is executed.
The XSLT model includes:
- XML document — Input data to be converted to other document types
- XSLT stylesheet — A well-formed XML document that contains a set of conversion rules and that’s used as a conversion template
- XSLT processor — An application that receives XML documents and XSLT styles as input data and performs a conversion by applying rules from XSLT styles to XML documents
- Output document — The result of a conversion
2. UML-based tools
Another possible way to convert code to a target language is by using tools based on Unified Modeling Language (UML) models. They generate source code in a particular language from UML classes and enable a UML model to display any changes in the source code.
Double-sided integration helps to synchronize source code and UML. This means every time you generate a piece of code or update a UML model, these changes will be merged.
Code generation using UML models is embedded in Microsoft Visual Studio. It generates code written in C# from UML class diagrams, which allows your developers to concentrate on the business logic and project architecture instead of writing low-level infrastructure code. It also helps your team avoid mistakes in code that are likely to occur during manual coding and that take a lot of time to debug.
To generate C# code out of UML class diagrams, use the
Generate Code command. By default, it creates C# code for each selected UML diagram. You can change or scale up this behavior by editing or copying text templates that generate code. You can also choose any other behavior for types included in various model packages.
There are also independent instruments for generating code with UML models, such as:
These tools support code generation in various programming languages, including Java, C++, and Python. Most of these modeling environments can be integrated with such development environments as Eclipse, NetBeans, IntelliJ IDEA, Visual Studio, and Android Studio. If you want to learn more about code generation with UML models in Microsoft Visual Studio, check out the official website for more details.
3. Razor Generator
Now, let’s take a look at one of the C# code generation tools.
Razor Generator (note that it has no connection to the Razor engine) is an open-source tool written in C# that supports Visual Studio 2022. It allows you to process Razor files at design time instead of at run time. Thus, you can integrate Razor files into your build in order to make it easier to reuse and distribute them.
Razor Generator provides developers with more time to launch and the possibility to test Razor views. You can learn more about this tool on its GitHub page.
Metadrone is a free code generation tool that uses a simple template syntax to output text based on a database schema. It allows developers to reduce the manual coding of:
- Interface screens
- Persistence layers
- ORM mappings in frameworks
- Stored procedures
- API classes
- And more
Metadrone supports SQL Server, MySQL, Oracle, and PostgreSQL databases.
Reegenerator is a commercial code generation tool integrated into Microsoft Visual Studio, with support for Visual Studio 2022. It can use any type of file as an input and generate any type of file as an output:
- Microsoft Office documents
- XSD schemas
- XML files
- Code files
- LINQ to SQL
Reegenerator uses several generators in a single file. The generators are regular C#/VB.NET classes in a regular .NET Class Library. To find out more about Reegenerator, visit its official website.
6. Т4 templates
The Text Template Transformation Toolkit (T4) is a code generator embedded in Microsoft Visual Studio since 2008. It supports design-time code generation for Visual Studio 2022.
Text Template is the element of T4 that generates output data in a Visual Studio project for every build. You can write template logic in C# or VB.NET. Transformation of design-time T4 templates happens during compilation. These templates are generally used for generating code when compiling the current project. After generation, T4 offers templates to a user. These templates can be reused by inheritance or inclusion.
Text templates consist of:
- Directives — Elements that manage template processing
- Text blocks — Content to be copied to the output
- Control units — Program code that inserts variable values to the text and manages conditional and repetitive parts of the text
Figure 2 represents an example of T4 operation, showing the command transformation and the application’s final output.
T4 interacts with Microsoft SQL Server. It also allows for generating custom database entities. The official documentation provides basic tutorials on how to generate code, and you can find a lot of user recommendations on third-party resources.
Radzen is a tool for web application development that generates code for the Angular and Blazor frameworks. For Blazor, Radzen even has its own Blazor Studio IDE, which helps to create applications from scratch. You can also work with Radzen files in Visual Studio Code.
The back end of Radzen is written in C#. When you add a data source from Microsoft SQL Server, MySQL, or PostgreSQL, Radzen creates an ASP.NET Core server application. Its pages are presented by the Model–View–Controller design pattern. You can edit such pages using C# partial classes and methods.
Radzen supports several databases:
- MS SQL Server
Radzen Studio also has built-in WYSIWYG design time functionality with the generated UI supporting localization and responsive design. This tool is updated frequently to support newer versions of UI frameworks.
The Radzen team offers extensive and informative official documentation, as well as the Radzen blog, webinars, and official community support.
8. CodeSmith Generator
СodeSmith Generator, part of CodeSmith Tools, is a source code generator based on templates. It automates code generation for any text language, including C#, Java, VB, PHP, ASP.NET, and SQL. This tool also offers a set of useful templates, including templates for dealing with verified architectures including:
Templates can generate code in any language based on the American Standard Code for Information Interchange (ASCII). You can easily change the default template or create your own. And once code generation is finished, CodeSmith Generator provides the results.
CodeSmith Generator interacts with databases using SchemaExplorer. This tool provides the generator with types of interactions with SQL server or ADO data and design tools for accessing those types from CodeSmith Generator.
Also, CodeSmith Generator includes a lot of database templates.
The official documentation isn’t extensive, especially compared to other generators. But on the bright side, the CodeSmith website offers several video tutorials.
Besides the generator, the CodeSmith tool set includes:
- Exceptionless — Creates reports on errors, functions, and logs in real time and works with applications written in ASP.NET, Web API, WebForms, WPF, Console, and MVC.
- CodeSmith Frameworks — Provides a developer with the PLINQO framework, a set of CodeSmith templates that generate Object Relational Mapping skeletons using conformed design templates. PLINQO frameworks include an extended feature set to simplify and optimize data access.
9. ASP.Net Zero
ASP.NET Zero is one of the open-source code generation tools for Visual Studio. It’s based on a multilayered architecture, with its code structure being based on SOLID. It’s also equipped with Metronic UI, which provides a convenient interface.
ASP.NET Zero ensures a highly capable and scalable architecture and pre-spawned pages. It also supports multi-tenancy, subscriptions, and payment systems. This generator has an admin panel with all functionality for system and user administration, including for adding log records to the database. Dynamic localization makes it possible to launch your project in several countries, and the dynamic interface allows for adapting it to various user classes.
ASP.NET Zero provides developers with different architecture/framework options:
- ASP.NET Core and an Angular-based single-page application (SPA) solution
- ASP.NET Core and a jQuery-based MVC solution
- MAUI & Blazor hybrid mobile application integrated with the backend solution
- ASP.NET MVC-based application for your public website
ASP.NET Zero launch templates work with SQL Server by default. You can adapt them for other data repositories manually. Also, if you use the integrated EntityFramework, you can adapt MySQL templates. You can look in the official documentation to see a manual for this process. If you use Entity Framework Core, you can integrate it with MySQL, PostgreSQL, or SQLite. It’s also possible to integrate it with other database types, but there are no instructions on how to do it yet.
ASP.NET Zero provides developers with the source code after purchasing a license. There are two exceptions:
- A lot of free and open-source libraries are provided as NuGet components. Libraries are not included in the code you get because they would take up too much space and it would be hard to update those libraries.
- There’s a NuGet component with closed source code included in ASP.NET Zero that ensures compliance with the license regulations.
The official documentation for each ASP.NET Zero version might not be very extensive compared to the documentation for other tools, and there are no additional webinars, blogs, or video tutorials. But there’s an officially supported community.
ASP.NET Zero features are especially useful for developers working for small and medium-sized businesses who have limited time and resources for creating software. They’re also helpful for SaaS application development.
Design-time code generation allows developers to save time, avoid errors when writing low-level code, and use the results of code generation when working on an application. To ensure such benefits, it’s essential to thoughtfully choose the code generation tool that best fits your specific project.
Although modern code generation projects try to leverage artificial intelligence algorithms, non-AI tools like those we’ve mentioned in this article are still efficient and worth using. At Apriorit, we have skilled specialists in custom web application development who are ready to help you choose the most suitable technology stack that will meet your project’s requirements.
Looking for a way to accelerate product development? Contact us and let’s start working on your project together!