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;

     }
}

lunedì 23 gennaio 2012

DAO and Factory Design Patterns

As already said in the previous post the purpose of the DAO patter is to decouple the persistence logic from the business logic. So that future changes to the persistence tier will not affect the business tier, therefore changing the persistence device will not affect the business logic.


From now on I explain how to use the Factory pattern to implement the DAO pattern:

  • a good idea is to start creating an interface containing the methods that we want implement in our daos, like the following one:   
    •          public interface GenericDAO {
    •                 public void save(Object object) throws DAOException;
    •                 public void update(Object object) throws DAOException;
    •                 public void remove(Object object) throws DAOException;
    •                 public Object findByPrimaryKey(Object pk) throws DAOException;
    •                 public Collection findAll() throws DAOException;
    •         }

  • and if we want we can extend the java.lang.Exception to implement our specific exception (DAOException) 

    •    public class DAOException extends Exception {
    •           public DAOException(String message) {
    •              super(message);    
    •           }
    •           public DAOException(Throwable e) {
    •              super(e);    
    •           }
    •      }

  • Then we need to implement the core class which implements the factory and singleton patterns: in this manner we will use  use always only one instance of our DAO (singleton pattern); while the factory pattern returns an interface, in this case the GenericDAO, that at run time will be a real class that implements that interface. Then we create a properties file which will be sued by the DAOFactory to discover the real implementations for DAOs that will be requested at run time by the application.
    • public class DAOFactory { 
    •     private static DAOFactory factory = null; 
    •     private Properties props = null; 
    •     private DAOFactory() {
    •         try {
    •             props = new Properties();
    •             props.load(DAOFactory.class.getResourceAsStream("daos.properties"));
    •         } catch (IOException e) {
    •             e.printStackTrace();
    •         }
    •     } 
    •     public static DAOFactory getInstance() {
    •         if (factory == null) {
    •             factory = new DAOFactory();
    •         }
    •         return factory;
    •     } 
    •     public GenericDAO getDAO(String name) {
    •         GenericDAO dao = null;
    •         try {
    •             dao = (GenericDAO) Class.forName(props.getProperty(name))
    •                 .newInstance();
    •         } catch (InstantiationException e) {
    •             e.printStackTrace();
    •         } catch (IllegalAccessException e) {
    •             e.printStackTrace();
    •         } catch (ClassNotFoundException e) {
    •             e.printStackTrace();
    •         }
    •         return dao;
    •     }
    • }   

Structure of the project

Here there is a picture of the architecture of the project that I will implement. 
Mainly the structure is divided into three tiers:
A persistence tier to manage the persistence of the data objects, a business tier, that contains all the logic used in the application  and the web tier where there are the html pages and the servlets that access to the business logic implemented in the EJB using the Business Delegate pattern.

So from this picture can be understand also the design patterns that I will adopt to develop the application. In particular I will use:

  • DAO pattern: has the purpose to separate the business logic from the logic to access the data.
  • DTO pattern: reduce the network overhead transmitting an entire entity in one call instead of transmitting  each field of the entity.
  • Session Facade pattern: expose an interface of the business logic, hiding the implementation  details and the complexity of the business tier. 
  • Service locator pattern: hide the mechanisms of the lookup to the client.
  • Business Delegate pattern: decouples business components from the clients.