mercoledì 25 gennaio 2012

DTO and DTOTransformer

Regarding the DTO pattern I suggest to create for each DTO an utility class containing the methods to convert the DTO to the correspondent entity and  inverse, from the entity to the DTO. These classes will be very useful in the implementation of the DAOs.

An example of a converter is shown here:


public ObjEntity dtoToEntity(ObjDto dto) {  
      if (entity == null)
            return null;
      } else {

           ObjEntity to = new ObjEntity();

          to.setField1(dto.getField1());
          to.setField2(dto.getField2());
          to.setField3(dto.getField3());
         

          return to;

     }
}


public ObjDto entityToDto(ObjEntity entity) {  
      if (entity == null)
            return null;
      } else {

           ObjDto to = new ObjDto();

          to.setField1(entity.getField1());
          to.setField2(entity.getField2());
          to.setField3(entity.getField3());
         

          return to;

     }
}

Nessun commento:

Posta un commento