More often we have domain driven design in our agile projects (with hibernate Dao and pojos as domain obejcts) and it tends out to be that we need a “base” object or a handle for both. In the past we used to write Daos which ended up doing pretty much eveything for multiple Domain Objects.
To approach this common problem with a better design, in our project we have an interface DomainObject and an abstract representation of it – AbstractDomainObject which every domain Object extends. Having a common handle for your domain objects is a very useful tool (you will realise it later). Unlike most of the other implementations of Daos, we have come up with a tight coupling (yet generic enough) to clearly dilineate workflows for Daos. Using generics one can at compile time couple the domain class with the Dao during instantiating the Dao class itself. Here is how it is done -
public interface BaseDao {
/**
* Get Object by Id (based on domain class)
* @param id identifieer
* @return domain object
*/
public T getById(int id);
/**
* Return the domain class reference in the dao
* @return
*/
public Class domainClass();
/**
* Interface to save a domain object
* @param domainObject the domain object
* @throws DataAccessException runtime exception
*/
public void save(DomainObject domainObject);
}
Here is an Abstract Implementation of the interface
public abstract class AbstractBaseDao extends HibernateDaoSupport
implements BaseDao{
//
}
And the concrete implementation of Dao -
public class StudyDao extends AbstractBaseDao {
@Override
public Class domainClass() {
return Study.class;
}
//other methods
}
As you see, the responsibility of each Dao class is clearly establishing it’s connection to a particular Domain Object.(compile time). Not only that, the various utility methods (which have been skipped in this section) in the Base Dao can use domainClass() method for the Dao to “know which domain object I am working on currently”. (runtime)
If you think about this design, it is actually tightly coupled and quite generic enough to handle any business model of your application letting to freely write your Dao classes without worrying too much about the internals of the domain object
AbstractBaseDao
AbstractDomainObject
Post a Comment