With the generic dao interface:
public interface GenericDAO<T extends DataObject, ID extends Serializable> {
T save (T object);
T load (ID id);
void remove (ID id);
Class<T> getPersistedType();
}
You implement GenericDaoImpl with your favorite ORM, like Hibernate to handle loading and persisting generic objects. You end up with UserDao, StoreDao, TransactionDao, PetsDao, and 50 other Dao that each have different logic on how the object is to be saved, loaded, and removed. You may also have many instances of the GenericDao, each for a class type that only requires generic handling.
I am looking to implement an service that will handle these operations for any DataObject and will choose the right Dao to do it. If there is an Dao implementation for this object, use it, otherwise use the GenericDao.
One way to do it is to have a DaoFactory if/else statement to check the object type, and return the corrosponding Dao. However, this is will require you to make modification to the factory each time a new Dao is added and not very elegant.
What’s a better way to implement this? To ask it another way,
how do you find the Dao that’s associated with particular class?
I once wrote a GenericHibernateDAOImpl; an instance of which could be used to perform read/ write or delete operation on types of any entity. I found the following links useful:
https://community.jboss.org/wiki/GenericDataAccessObjects?_sscc=t
http://www.ibm.com/developerworks/java/library/j-genericdao/index.html