To generate or not to generate? Each developer has a different opinion on generating code at the design stage. We say generate but with the right tool.
Code generation at the design stage saves a lot of time for a developer. The right tool quickly translates your code into a low-level language with few or no errors.
In this article, Apriorit experts review the nine most popular code generation tools. We compare languages, functionality, performance, availability, official support, and other characteristics. This article will be useful for developers who are looking for the best design-time code generation tool for their project.
Code generation allows a developer to concentrate on a high level of abstraction during development. It transforms high-level code written by a human into a low-level language. In other words, it generates source code based on the description or model of our project. This allows us to save time during application development and protects our 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. You’re free to use any compiler you find effective for your task. However, there are dedicated tools for converting your high-level code into low-level code. Here are some of the top design-time code generation tools:
- eXtensible Stylesheet Language Transformation (XSLT) templates
- UML-based tools
- Razor Generator
- Text Template Transformation Toolkit (T4) templates
- CodeSmith Generator
- ASP.Net Zero
All of these tools are useful when you need to generate simple repetitive code (for example, a client proxy for OData) or any text according to a template.
However, you can also consider using the Roslyn compiler if:
- you need to generate C# or VB.NET code
- you’re creating a code generator
- generation logic is more important than the resulting code template
Let’s take a closer look at the different tools you can use for design-time code generation.
The choice of a design-time code generation tool depends on:
- the language used
- the type of application you need to create
- the development environment you use
Let’s take a look at solutions you can use to generate source code for your software.
eXtensible Stylesheet Language (XSL) is a styling language for XML documents. eXtensible Stylesheet Language Transformation (XSLT) is a 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 other 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 constructions 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 nodes of incoming XML. 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 types of documents
- XSLT stylesheet — A well-formed XML document that contains a set of conversion rules and that’s used as a template for conversion
- 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
It’s possible to convert code to a target language 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 with UML models is embedded in Microsoft Visual Studio. It generates code written in C# from UML class diagrams. This allows you to concentrate on the business logic and project architecture instead of writing low-level infrastructure code. It also helps you avoid mistakes in code that inevitably occur during manual coding and take a lot of time to debug.
In order 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 code generation with UML models: UModel, Visual Paradigm, Modeliosoft, Enterprise Architect, erwin Data Modeler, etc. 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, investigate the official website.
Razor Generator (note that it has no connection to the Razor engine) is an open-source tool written in C# that supports Visual Studio 2019. It allows a developer to process Razor files at design time instead of at run time. Due to this, you can integrate Razor files to your build in order to make it easier to reuse and distribute them.
This tool provides a developer with more time to launch and allows for testing of Razor views.
Metadrone is a free tool that uses a simple template syntax to output text based on a database scheme. It allows a developer to reduce manual coding of interface screens, persistence layers, ORM mappings in frameworks, stored procedures, API classes, etc. Metadrone supports SQL Server, MySQL, Oracle, and PostgreSQL databases.
There’s detailed documentation of its functionality.
Reegenerator is a free code generation tool integrated into Microsoft Visual Studio. It can use any type of file as an input and generate any type of file as an output:
- Data files: XML, JSON
- Code files: CS, .VB
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.
Text Template Transformation Toolkit (T4) is a code generator embedded in Microsoft Visual Studio since 2008. Text Template is the element of T4 that generates output data in a Visual Studio project for every build. Template logic can be written in C# or VB.NET. The transformation of design-time T4 templates happens during compilation. These templates are generally used for code generation during current project compilation. After generation, T4 provides a user with templates. 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, where steps 1 and 2 show the command transformation and step 3 shows the final output of the application.
T4 interacts with Microsoft SQL Server. Also, it allows for generating custom database entities. The official documentation provides basic tutorials on how to generate code. There are also a lot of user recommendations on third-party websites.
Radzen is a tool for web application development that generates code for the Angular framework. You can work with Radzen files in Visual Studio Code. This tool is especially useful for small projects due to its limited functionality.
The backend 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
There’s extensive and informative official documentation as well as the Radzen blog, webinars, and official community support.
СodeSmith Generator is a part of CodeSmith Tools. This is a source code generator based on templates. It automates code generation for any text language. CodeSmith Generator contains a set of useful templates, including templates for dealing with verified architectures (netTiers, SLA, NHibernate, PLINQO, Entity Framework, Kinetic Framework, etc.). You can easily change the default template or create your own. When generation is finished, CodeSmith Generator provides the results.
CodeSmith Generator supports C#, Java, VB, PHP, ASP.NET, SQL, and other languages. Templates can generate code in any language based on ASCII.
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.
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.
ASP.NET Zero is a solution for Visual Studio. It’s based on the multilayered architecture, while its code structure is based on SOLID. It’s also equipped with Metronic UI, which provides a comfortable 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 us with various framework options:
- ASP.NET Core 2.x and Angular 7.x-based Single-Page Application (SPA) solution
- ASP.NET Core 2.x and jQuery-based MVC solution
- ASP.NET MVC 5.x, ASP.NET Web API, and AngularJS 1.x-based Single-Page Application (SPA) solution
- ASP.NET MVC 5.x, ASP.NET Web API, and jQuery-based solution
- Xamarin mobile application integrated with a backend solution (only for ASP.NET Core (MVC or Angular UI) versions; supports iOS & Android)
- ASP.NET MVC-based application
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. There’s a manual for this process in the official documentation. 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.
ASP.NET Zero provides us with the source code after purchasing a license. There are two exceptions:
- The tool contains a lot of free and open-source libraries as NuGet components. They are not included in the code you get after purchase because it would take too much space and would be hard to update those libraries.
- There’s a NuGet component with closed source code included in ASP.NET Zero. It protects the license regulations.
The documentation for each ASP.NET Zero version is available on the official website. It’s not 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.
These 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 useful for SaaS application development.
Caching in .NET and SQL Server Notifications
Design-time code generation allows developers to save time and avoid errors when writing low-level code. Also, you can use the results of code generation when developing your application.
There are a variety of tools for design-time code generation. Some of them are embedded in popular development environments like Microsoft Visual Studio, while others are independent solutions. The choice between them depends on lots of parameters:
- Which environment, language, and framework do you use?
- What features do you require?
- What’s your project budget?
- And so on.
In this article, we reviewed nine key tools for code generation at the design stage. Still not sure which one to choose? Contact our development team and start discussing your project!