Case Study: strategies to do data binding and value building in the application tier
(by Mark Qian on 10/31/2006)
Introduction
One of the major tasks for the application tier in a wab application is the serialize and deserialize data between
browsers (a plain-text format, the html document) and the target data beans in the appilcation tier (an object-oriented format), the translation between paremeters in HttpRequests and data beans that you use to
invoke API in next tier. When doing the translation, people have been struggling amoung performance, maintainability
and flexibility. The goal of this study is to find the suitable way to satisfy most of the struggling issues
In this article, I will first show you several approaches people have been doing and then bring up my solution.
Since the situation is similar in vareity of web application frameworks such as Struts, JSF, Spring MVC and so on,
I will use Spring MVC as my environment.
I will assume that we have a relatively "complicate" case where a single page contains content from two different
data beans at the business tier, in our case, Bamboo and Source. (We will see the live page below in details)
You will see a "Bamboo Administration" page when clicking at links "See the page with live code here" below. In the page,
there are two subframes, list and edit. In the lower subframe, the edit area, you can modify the info about
each selected (from the upper subframe) bamboo and select its whole sale source. What we are talking about for this
study is the Save action. When users click at Save button, we submit content from two business data beans, Bamboo and Source.
Note: there are some other issues you may want to take a look on the pageShow details
- DOJO event system is used on the edit frame.
- Dependent section (the Source Data) is dynamically loaded using AJAX in the edit area.
- Tableless layout in the Edit area
The Problem:
We have two objects (Bamboo and Source) in the edit page but HTTPRequest flats them into key-value pairs. How do we
"translate" those parameters into business objects Bamboo and Source after we receive the requset?
The approaches appear as two extrame cases:
- The old "ActionForm" style
In this approach, the old way Struts people did in their actions, a "ActionForm" (BambooAdminEditForm0Bean), is used to
contain two flatted objects (other web frameworks also have similar "data storages" such as component tree in JSF, "pages" in Tapestry, that you can not use directly to invoke APIs at next tier). Then in the handler (BambooAdminEditForm0Controller), I "menually" do the value building. That is, I call the getters in BambooAdminEditForm0Bean
and call the setters of Bamboo or Source to fill up these two objects so that I can use them to invoke APIs at next tier. See the problem diagramShow details
The advantage is
1. BambooAdminEditForm0Bean isolates the business info from presentation layer so that business objects Bamboo and Source never appear in the presentation layer, bamboo_admin_edit0.jsp.
The disadvantages are:
Source CodesShow details
- The Direct-Binding style
The Solution:
So, is there any better solution? Yes .
Note: "better" here means that it should have 1). no hard-coded section at all (in other words, no change should be made for value building/loading in application tier
when thins are changed in the business tier.), 2). no wastful "ActionForms", 3). completely isolate presentation from business.
Related Source Codes