views:

104

answers:

2

I've noticed that having a data access object (DAO) interface and implementation is really starting to add up:

public interface ForceDAO{
    public void store(Force force);
    public void delete(Long forceId);
    public Force findById(Long forceId);
    public List<Force> findAll();
    public void deleteAll();
}

I have this same interface an implementation for each of my entity classes. Before I get crazy with a refactoring, I'm wondering if there are any suggest patterns to apply here? I had a few ideas of my own:

  1. Use the template pattern to factor out all of the boilerplate code and delegat to specific DAO's when needed. This may reduce code, but I think the number of interface and class files would be the same.

  2. Describe the interface and implementation using generics, then parameterize the implementation with the specific DAO I need. Again, this would reduce code but I think the number of files would remain the same. Also, I"m not sure how this would work as my generics skills aren't strong yet. This is my preferred choice so far though. Is anybody else using a similar strategy?

  3. Create an abstract base DAO class that implements common functionality, then extend it with the more specific DAO classes for the details. This would save me the trouble of creating so many interfaces, although the names of DAO methods could not be specific to an entity (e.g i couldn't use findDogById(), it would have to just be findById() ).

  4. Finally because different access schemes may be involved when retrieving data (Hibernate, JPA, iBatis etc), it would seem like swapping implementations is desirable. This fits well with the strategy pattern.

What is the most elegant and efficient approach to developing DAO's? Which approach best favors reuse and minimizes redundancy?

+4  A: 

You could look at generic dao.

fastcodejava
neat, this is what I had in mind. On a side note, I've noticed more and more, how many good articles are on the IBM developer site. Its not normally a place I visit but I think i'll be checking it out more often.
darren
You might also look the eclipse plugin for this kind of projects : http://fast-code.sourceforge.net/.
fastcodejava
generic dao is definitely the answer to the question
nanda
+2  A: 

In your situation, I'd look to see if there was an existing model-based code generation system that could be easily applied to the problem. Especially if you've got 50+ of these classes to write.

Hibernate, JPA, etc are also attractive alternatives, especially to the extent that you can use annotations, etc to avoid writing those DAO classes at all.

My final fallback would be to use a combination of generics and abstract base classes.

Stephen C
What do you mean by using JPA / Hibernate annotations to avoid writing DAO's? I am using the JPA annotations on my entities, but I still need DAO's to store(), delete(), findById(), findAll() etc, don't I?
darren
Cent percent agreement, I don't think how DAO fits in with Hibernate and JPA thingy, especially when using container managed transaction i.e. JTA.
Adeel Ansari
OK ... I admit that my knowledge of Hibernate, JPA, etc is pretty limited. But I thought that they largely avoided the need to explicitly code DAO classes. (Or perhaps I'm confusing JPA with JDO).
Stephen C