Which methods to implement into a "correct" DAO class?

I know the title of this question is not very descriptive, but I didn't know how to better explain the question...

I'd like to know your opinion about a doubt that arised during the implementation of a DAO class.

I'm implementing a CustomerDAO class that must provide access to related data in my application DB. I've implemented the classic CRUD methods as usual, but now I need to implement some method providing data retrieved used "specific" queries.

I mean something like:

SELECT [...] FROM CUSTOMERS WHERE <CUSTOMER_PROPERTY_1> = 'X' AND <CUSTOMER_SOME_DATE> > ? AND <CUSTOMER_SOME_DATE> < ?

So my question is, what's the right approach or "best-practice" to follow? Implement many specific methods like getCustomersByXPropertyBetweenDates() or try to generalize query and implement a more "generic" (not in Java-sense) method?

Note I'm using plain JDBC and JdbcTemplate provided by Spring Framework v 3.1

Answers


How would you make the query you listed "generic" ? Also, bad choice of word.

I would just have the separate queries, separate. Don't try and do anything clever, you have a specific query keep it simple don't try and fit in around another query.

If you want properly generic I'd suggest using an ORM.


I think it is more business driven decision. If you have separate business operations related to different methods, you could create the specific methods. If you have business operation "advanced search form" with pack of fields for search criteria, it has more sense to create a special "query" entity class with the form data, which will be then converted into a sql query by DAO implementation.

Also, please have a look at the query dsl, it may simplify a lot of the DAO layer.


Implement an abstract DAO class which defines/implements methods common(CRUD) to all the DAO's. Define business methods in specific DAO interfaces and implement in DAO classes.

public interface IDAO< T extends Serializable>{

   T save( final T entity );
   T update( final T entity);
   void delete( final long id );
   void deleteAll();
   T findOne( final long id );
   List< T > findAll();

}

public abstract class AbstractDAO< T extends Serializable > implements IDAO< T >{




protected abstract JdbcTemplate getTemplate();


protected abstract T save(T entity);

    ...

}

customer DAO interface will define business specific methods

public interface ICustomerDAO extends IDAO<Customer>
{
   public List<Customer> findDormantCustomers();

}

customer DAO implementation will look something like below.

public class CustomerDAO extends AbstractDAO<Customer> implements ICustomerDAO{

public Customer save(Customer customer){
   getTemplate().insert(...);
   return customer;
}

    public List<Customer> findDormantCustomers(){
       ...
       return;
    }
}

Need Your Help

JQuery Mobile Date picker not showing date in Chrome

asp.net vb.net html5 google-chrome asp.net-mvc-4

I have an MVC 4 site using JQuery Mobile. I'm using a model with an EditorFor to render the date editor. in MVC I have this:

Given an XML stream how can one know whether it is a binary XML or non binary XML stream?

.net xml

I have a stream which contains some XML. The XML may either be encoded using binary XML writer (for instance obtained from XmlDictionaryWriter.CreateBinaryWriter) or non binary XML writer (like as in