Need for a separate interface and import for DAO

We have a typical n-level Java application, and I noticed that our data access levels have DAOs that are of type FooDAO and FooDAOImpl. I was looking for an excuse for these two, and here is my analysis.

  • If you had multiple implementations for the same interface, abstraction is useful. But considering that we have already made the choice of the structure that will be used for DAOImpl (say, iBATIS), is this really necessary?
  • Proxy help through Spring. From what I'm compiling, classes that have interfaces can be approximated easily (along the JdkProxy path), rather than classes that don't have interfaces (where the cglib route is chosen), and one has a subclass of the class that needs to be proxied. The subclassification has its problems when the proxy class is final or does not have any default constructors - both of which are extremely unlikely at the data access level. Performance was a factor, but from what I hear it is no longer a concern.
  • Help mockingly. Classes with interfaces are more suitable for bullying due to mocking frameworks. I only heard it, but did not see it in practice - so in fact you can’t count on it, but perhaps because of the same factors that were mentioned in No. 2 above.

With these points, I don’t feel the real need for a separate FooDAO and FooDAOImpl, where a simple FooDAO is enough. Feel free to fix any of the points I mentioned.

Thanks in advance!

+8
java spring dao mocking
source share
3 answers

I tried # 3 with Mockito and it was able to mock POJO without interfaces. With the arguments pointed out against No. 1 and No. 2, I am inclined not to go with separate DAO and DAOImpl. Feel free to add other comparison points.

+2
source share

I see the fourth reason:

  • Hide implementation details

Depending on the team, the expected life of the software, or the expected number of changes in the future, it pays to hide as much as possible, even if there is only one implementation.

+1
source share

I would say that having a FooDAO interface with one implementation of FooDAOImpl usually an anti-pattern. A simpler solution (without separate interfaces for the DAO) is much preferable if you do not have a good reason - this does not seem to be the case.

Personally, I would go even further and say that having a DAO is generally not the best choice. I prefer to have a single facade of the durability class implemented on top of an ORM API such as Hibernate or JPA. Perhaps iBATIS is too low level for this.

0
source share

All Articles