|
Mapping Data Objects Simply
Take a look at a simple technique for mapping data objects from HTML to database and back in Web applications
by Probir Goyal
August 4, 2004
Data Objects in real world Web applications follow two general paths: one from user input to database and the other from database to output (HTML), with a business process layer in between (see Figure 1). While developing these applications, a remarkable amount of time and energy goes into mapping the data to objects, either from HTML forms to database or from database to HTML. These mapping processes generally do not have any business intelligence associated with them, and they simply follow the standard way of calling getter and setter methods. On the implementation side, these processes are tedious and are error prone. Object-mapping techniques and tools can really help in reducing the mapping pain, not to mention saving time and energy to concentrate on real business problems.
The philosophy of mapping objects is not new; in fact, there are quite a few tools and frameworks available in the market that can do object-relational mapping quite well. The purpose here is not to advocate for or against these tools, but to provide a simple lightweight technique that can be used in any Web project, without any extra learning curve or license issues.
With a little help from the Reflection API, we can write simple methods, which can do the mapping for us automatically. The key to this mapping automation is to use the same field names across the project, that is, in HTML form, in a data bean, and in a database table. Introspection using reflection allows the code to locate the data fields and methods associated with them at runtime. These methods are invoked to set or get values from any anonymous data bean object, without any knowledge of the data bean object type.
JSP/Servlet containers provide a list of HTML form elements with the input request. The mapping process traverses this list and maps each element to the data bean's field. Listing 1 shows the process as a function that is called with the input request and the data bean to be populated.
The first step of introspection is to get the class information about the object in question—in our case it's the data bean. The class Class holds complete meta information about the object it refers to. For our mapping technique we are only interested in two methods provided by the Class. The first one is getDeclaredField(fieldname), as the name suggests. It returns a Field object, which further provides the information about the type of field. The second one, getMethod(methodName, parameterTypes), returns a Method object, which is invoked to set the field values of the data bean.
As mentioned previously, the key to this mapping is the common field names; the code shown in Listing 1 uses the field names from the HTML form as a key to locate the corresponding field in the data bean. Also worthy of note is how the Method object passes parameters while invoking the methods on the data bean. All parameters are passed as objects, which requires the conversion of parameters from the HTML form to corresponding objects, depending on the data type each parameter is representing. Listing 1 shows this conversion process.
Back to top
|