Code Generators for Rapid Web Development

As a web developer, one thing that helps me to rapidly develop web applications is to use a common application framework that is flexible and robust. Additionally, I like to use code generators to build code for custom applications I build for my client. My most powerful code generators, create code for interacting with the local database dedicated to my website.

Normally, it is bad practice to repeat code when doing development. However, there are certain instances when this can be beneficial and assist in creating dynamic web applications. Here, we will discuss some of the many applications that I have found useful and how you can apply them to your own business.

Object-Oriented Classes

One way I enforce code reuse is by using object-oriented design. For my data access layer I create an abstract class which contains the common functionality. Next, I create derived classes which implement the specific methods which are needed for the entity model (usually a database table).

These derived classes have different fields which represent the fields defined for the table. They also contain mappings for the primary keys, any related fields that are retrieved from related tables, and custom methods for querying the database. The idea is that all of the database calls are encapsulated in the data access layer classes.

These derived classes have enough similarities between one another that it made sense for us to build a code generator to create these files from the database schema.

How to Generate Code in Your Intranet

On our intranet, we have the code generated connected directly to our database management scripts. When an administrator is viewing a table schema, they have a button on the bottom of the screen to generate the code for our data access layer. When the user presses this button, the code is immediately generated and the user can click anywhere on the code to select the code block and copy it to the clipboard.

The process of generating code is surprisingly simple. We simply retrieve the schema from the database and from that we define all the macros that are needed to substitute into a code template. These macros include things such as the script name, database table name, primary key fields, public fields, private fields, and a generated class name.

The code is output to the screen as pre-formatted text. Below this is a web form where the user may tweak any of the macro values that were generated. After making changes to these values, they can click a submit button which regenerates the code using the custom macro values. Of course this step is optional. The user may simply choose to copy all of the program code and paste it in their code editor and continue making changes that way.

Table Administration

In my website administration panel, I have a lot of pages that are built for managing database tables. I have a very capable library which handles all of the heavy lifting for paging through a table of records, creating a new record, editing and deleting a record. This is an object-oriented class that takes a variable number of parameters.

To create a new administration area, I just need to instantiate this class, define all of the required properties, and then call a method called “Process”. The resulting file is usually no longer than 25 lines of code. Creating these files doesn’t take very long when done by hand. However, I knew that creating a code generator for these server-side scripts would save us a lot of time.

Again, the key to accomplishing this goal was to first read the database schema for a table to get all of the field definitions. From these definitions, it would be a simple matter to create the code from an existing script template. I just define macros for all of the properties I need to substitute in the template. As the table schema is read, I build these properties which are later substituted in to the template.

Special Considerations

When generating code, it is important to keep in mind how the script is going to be used. In my data access layer scripts, I know that they are usually two directories beneath the website root. Because of this, I know that any relative links need to go up two levels to get to the site root.

Another important area to consider is form validation. There are certain constraints you can place on a web form to limit the amount of characters a user enters into a text field. You can even make Boolean fields display as radio buttons labeled “Yes” and “No”. Date fields can display using a specialized date picker.

Other special data fields can be displayed based on the field name. For example, fields containing the word “Password” can be displayed as password fields. I use fields with the name “created” and “modified” to track when a record has been changed. Fields that have the text “email” could be validated to make sure they contain a valid email address. Also, fields that have the text “postalcode” could be tested for valid postal codes.

I try to build my code generator so it is as smart as can be. The thinking behind this is that the developer can easily remove extra code that was added if they find too much validation is being done or the wrong type is done. The more work you can save for the developer, the better off you will be in the long run.