As a web developer, one particular point that helps me to quickly develop web applications is to use a common application framework that is versatile and robust. Furthermore, I like to use code generators to create code for custom applications I create for my client. My most powerful code generators, create code for interacting with the regional database dedicated to my website.
Normally, it is bad practice to repeat code when undertaking development. On the other hand, there are particular instances when this can be useful and assist in generating dynamic web applications. Here, we will go over some of the lots of applications that I have discovered beneficial and how you can apply them to your own company.
Object-Oriented Classes
1 way I enforce code reuse is by making use of object-oriented design and style. For my data access layer I generate an abstract class which contains the common functionality. Next, I produce derived classes which implement the certain techniques which are needed for the entity model (usually a database table).
These derived classes have diverse fields which represent the fields defined for the table. They also contain mappings for the principal keys, any associated fields that are retrieved from connected tables, and custom procedures for querying the database. The idea is that all of the database calls are encapsulated in the information access layer classes.
These derived classes have enough similarities between one a further that it made sense for us to make a code generator to develop these files from the database schema.
How to Produce Code in Your Intranet
On our intranet, we have the code generated connected straight 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 information access layer. When the user presses this button, the code is immediately generated and the user can click anyplace on the code to choose the code block and copy it to the clipboard.
The course of action of producing code is surprisingly uncomplicated. We basically retrieve the schema from the database and from that we define all the macros that are required to substitute into a code template. These macros involve points such as the script name, database table name, main essential 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 kind where the user might tweak any of the macro values that had been generated. After making changes to these values, they can click a submit button which regenerates the code employing the custom macro values. Of course this step is optional. The user may simply select to copy all of the program code and paste it in their code editor and continue producing changes that way.
Table Administration
In my web site administration panel, I have a lot of pages that are built for managing database tables. I have a pretty capable library which handles all of the heavy lifting for paging by way of a table of records, generating 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 region, I just need to have to instantiate this class, define all of the needed properties, and then call a method named “Method”. The resulting file is normally no longer than 25 lines of code. Creating these files does not take pretty long when carried out by hand. Even so, I knew that building a code generator for these server-side scripts would save us a lot of time.
Once more, the crucial to accomplishing this goal was to initial study the database schema for a table to get all of the field definitions. From these definitions, it would be a simple matter to make the code from an existing script template. I just define macros for all of the properties I require to substitute in the template. As the table schema is study, I create these properties which are later substituted in to the template.
When generating code, it is important to maintain in thoughts how the script is going to be utilized. In my data access layer scripts, I know that they are commonly two directories beneath the internet site root. Since of this, I know that any relative hyperlinks need to have to go up two levels to get to the web site root.
A further critical location to look at is form validation. There are certain constraints you can place on a web kind to limit the quantity of characters a user enters into a text field. You can even make Boolean fields show as radio buttons labeled “Yes” and “No”. Date fields can show making use of a specialized date picker.