DAO package structure

When designing an application there is no standard way of structuring in packages, experience is what usually helps every one to decide what are the appropriate names for our packages.

About packaging implementations of your interfaces in the same package or in a different one just think about how Java itself is structured: usually an implementation class is packaged in the same package that its interface, but is not all the times.

If you were about to have several implementations of the same DAO's then it would make sense having them structured in .jdbc, .jpa or .jdo sub packages. If your are only going to have one implementation both of the options you enumerate make sense in some way (same package or a .impl sub package).

Regarding over-engineering I would recommend you this article. Even though you are going to have just one implementation of your DAO's, it would make sense to have them defined as an interface and implementation as that will help you in a potential future to rewrite your DAOs for other frameworks whilst the code that makes use of them keeps unchanged.

At the end it's up to you (or you and your peers) to reach a consensus and make the decision that makes more sense in your specific case.

EDIT

An application usually has one implementation per DAO interface and that isn't over-engineering at all, it simply doesn't make sense to have the same DAO interface implemented for JPA and for JDO. Some of the purposes of using the interface/implementation pattern is to ease re-factoring, testing by means of mock objects, etc..

P.S.: I usually rely on JDepend to distribute my application classes in packages avoiding cycles as most as I can.


I don't think either is better, but in this case I prefer the first alternative. It would be in line with having ArrayList, LinkedList, etc. , in the same package as List.

When using additional frameworks, such as hibernate I prefer the second option with MyDao and HibernateDao as the implementor.


I would go with your second option (although none is really better), because you can see immediately in your imports if an impl is imported and refactoring would be simpler if you want to move your impl in another project.

This is not over-engineering. There are multiple advantages to use DAO:

  1. It improves the quality of your code by decoupling database access from other considerations
  2. Testing your code is made easier and you can test it with a finer grain.
  3. If some day, you find out that Hibernate is actually a lot easier for you, it won't impact the rest of your code.

Tags:

Java

Dao