Health Fitness

Code generators for rapid web development

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

Normally, it is bad practice to repeat code when doing development. However, there are certain cases where this can be beneficial and help build dynamic web applications. Here, we’ll discuss some of the many apps I’ve 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 that contains the common functionality. I then create derived classes that implement the specific methods needed for the entity model (usually a database table).

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

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

How to generate code on your intranet

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

The code generation process is surprisingly simple. We simply retrieve the schema from the database and from there define all the macros that are needed to substitute in a code template. These macros include things like the script name, database table name, primary key fields, public fields, private fields, and a generated class name.

The code is displayed on the screen as preformatted text. Below this is a web form where the user can modify any of the macro values ​​that were generated. After making changes to these values, they can click a submit button that regenerates the code using the custom macro values. Of course, this step is optional. The user can simply choose to copy the entire program code and paste it into their code editor and continue making changes that way.

Administration table

In my website admin panel, I have many pages created to manage database tables. I have a very capable library that handles all the heavy lifting for browsing 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 admin area, I just need to instantiate this class, define all the required properties, and then call a method called “Process”. The resulting file is usually no more than 25 lines of code. Creating these files doesn’t take 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 achieving this goal was to first read the database schema of a table to get all the field definitions. From these definitions, it would be very easy to create the code from an existing script template. I just define macros for all the properties that I need to override in the template. As the table schema is read, I construct these properties which are then substituted into the template.

Special Considerations

When generating code, it is important to consider how the script will be used. In my data access layer scripts, I know it’s usually two directories below the root of the website. Because of this, I know that any relative link must go up two levels to reach the root of the site.

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *