Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Hibernate DAO naming convention?

Is it typical to name DAOs in the following way:

UserDAO - interface
UserDAOImpl - implements UserDAO

I am wondering if its standard to use the suffix 'Impl' for the implementation or if something more meaningful is the best practice. Thanks.

like image 593
oym Avatar asked Jan 31 '10 21:01

oym


2 Answers

There are two conventions that I've seen:

  1. FooDao for the interface and FooDaoImpl for the implementation
  2. IFooDao for the interface and FooDao for the implementation

The former has its roots in CORBA; the latter is a Microsoft COM/.NET convention. (Thanks to Pascal for the correction.)

"Don't Repeat the DAO" is a fine idea. I personally think that article is more complex than it needs to be. There's a way to do it without reflection in finders that I happen to prefer. If you use Hibernate, query by example can be a great way to do it simply. The interface would look more like this:

package persistence;

import java.io.Serializable;
import java.util.List;

public interface GenericDao<T, K extends Serializable>
{
   T find(K id);
   List<T> find();
   List<T> find(T example);
   List<T> find(String queryName, String [] paramNames, Object [] bindValues);

   K save(T instance);
   void update(T instance);
   void delete(T instance);
}
like image 158
duffymo Avatar answered Nov 09 '22 22:11

duffymo


That is generally what I use. Sometimes the Default prefix like DefaultUserDAO might make more sense if you're creating an interface that you expect others to implement but you're providing the reference implementation.

Most of the time I feel those two can be used interchangeably but in some situations one provides a little more clarity than the other.

like image 35
Kevin Avatar answered Nov 09 '22 21:11

Kevin