Wednesday November 02, 2005
Using DTO patterns in NetBeans I hope that everybody knows Data Transfer Object pattern. If not, I will describe this very often used pattern in next few sentences. A Transfer Object (TO) is designed to optimize data transfer across tiers. Instead of sending or receiving individual data elements, a TO contains all the data elements in single structure required by the request or response. TO is a serializable plain old Java object that contains several members to aggregate and carry all the data in a single method call.
The TO is passed by value to the client. Therefore, all calls to TO are made on copies of the original Transfer Object.
I implemented new action in NetBeans 5.0 that generates DTO for CMP beans. You can select CMP bean in project tab, righ-click and invoke Generate DTO action.

public class CustomerDTO implements java.io.Serializable {
private java.lang.Long newId;
private java.lang.String lastZmenaName;
private java.lang.String firstName;
private boolean dirty = false; //dirty flag
private void setDirty(){
dirty = true;
}
public boolean isDirty(){
return dirty;
}
public populate(Object o){
.....
Now, I will explain all methods that are generated for Transfer Object and how these methods can be used. For all private fields set/get methods are created. I used for generation of TO other common store optimization (Dirty Marker) strategy. What does it mean? In all setter methods the isDirty flag is set to true. How can I use this feature in my design? For instance, I create some Transfer object in application tier and then send to web tier. There the object can be modified and returned back to application tier. How can I resolve whether the object was changed and therefore changes should be persisted? Very simple way, e.g.
public void saveCustomer(CustomerDTO cust){
if(cust.isDirty()){
// save Customer
}
}
public CustomerDTO getCustomerById(int id) throws Exception {
CustomerDTO custTO = new CustomerDTO();
CustomerLocal customerBean = lookupCustomerBean().findByPrimaryKey(new Long(id));
custTO.populate(customerBean);
return custTO;
}
Note, NetBeans action generates Transfer object only for selected beans not for referenced beans, this is only one drawback of this feature. Relationships between TOs should be added manually.
Posted by pblaha
( Nov 02 2005, 10:39:12 AM CET )
Permalink
Comments [1]