In day to day life, a programmer spends a lot of valuable time on writing repeated code. These repeated codes are known as boilerplate. Writing a lot of boilerplate hampers the productivity of a developer. There are a lot of Code Generators that help to reduce the valuable time spent on writing these boilerplates, for example Lombok Yeoman, Telosys, etc.
In Modern Distributed Application, there can be a lot of microservices and these microservices have CRUD operations/web UI. These CRUD operations have repeated codes and business logic. Once a microservice is standardized and reviewed by the team, later we can use it as a template and generate similar microservices in an effective manner.
Code generator utility is one of the most sought after tools in a product development organization. It helps to generate or develop code quickly and provides flexibility to make code consistent with the standards at the organization level. We can also avoid writing repeated similar code.
Telosys is one such free, powerful code generator tool usable with different languages and frameworks. This tool can be easily plugged-in with IDE and also has command line support. To write the template and generate a template bundle we can use Eclipse. These templates are written in Velocity Template Language. Once templates are ready then by the help of the Command Line Interface (Telosys CLI) we can configure through jobs (Jenkins or other) or generate the code with only CLI (IDE is no longer required).
In this post, we will illustrate how to use this tool to create the Rest API CRUD operations for the Employee person entity. In the end, we will generate classes with unit test cases for Rest Web service following MVC architecture.
Building Code Generator Tool:
Download and setup Telosys:
- In your eclipse go to Help - Install New Software -> search https://www.telosys.org/eclipse/v211
- Select the checkbox next to Uncategorized -> Click Next
- Complete the next steps and click on Finish
- Restart Eclipse
Config Telosys with project:
- Right-click on your project’s core package and go to properties
- Go to Telosys tools tab on the left and press “Init Telosys Tools”
- Once it is done then Telosys Tools will be available in your core module
- Add the bundle file inside the templates folder.
- This bundle is a collection of templates that will be used to generate Rest Microservices CRUD operations with unit test cases.
- Update telosys-tools.cofig file
- Edit this file and edit your microservice specific properties. Example – client path, microservice name, DTO path
- The project structure should be matched with the config file variable.
- Open the dbcfg file and provide database connection and click on Generate Repository (Tools supports multiple databases).
- This file also supports an updated database table with the help of an update repository
- Now MYSQL.dbrep file is generated. This file contains the metadata of database tables
- Under table/Entity name dropdown, select the table for which you want to generate entity classes.
- In Entity Class text box, enter the name in which you need the entity class to be generated.
- Configure the templates.cfg with all the available files in the bundle.
Setup is ready to generate the classes for the entity.
Now go to the MYSQL.dbrep and click.
- Now go to Bulk generation tab in the bottom and checkmark all the tables for which you want to generate classes.
- Now, on the right pane, click on the dropdown and select the template based on the category your entity belongs to.
For more information and reference: