当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Extending DAO with JDBCDAOSupport, a bad practice?

问题描述:

i have seen a lot of examples on the internet and in real life. That people extend their DAO with JDBCDaoSupport while using Spring framework. It is conceptually very much wrong as extends or inheritance is an "is a" relationship. While our DAO is DAO it is not DAOSupport of whatever kind. By Extending it with JDBCDaoSupport we make it a JDBCDaoSupport. If someone wants to use JDBCDaoSupport he should use it by composition not by inheritance. Am i right?

网友答案:

Are you sure you are not referring to Spring's JdbcDaoSupport?

From the javadoc:

Convenient super class for JDBC-based data access objects. Requires a DataSource to be set, providing a JdbcTemplate based on it to subclasses through the getJdbcTemplate() method.

In terms of implementation decision why JdbcDaoSupport is using internally inheritance rather than composition that's because DaoSupport is an abstract class. This is textbook inheritance since DaoSupport serves as a skeletal support implementation to be subclassed by Jdbc/Hibernate/Cci DaoSupport classes.

In terms of why we are extending our Daos from JdbcDaoSupport rather than using composition that's so we can get getJdbcTemplate() or getDataSource() directly rather than having to create our own infrastructure to expose them. In such case we don't need such support class from Spring since our Dao implementations are in a sense doing all the work. These *DaoSupport classes offered by spring are a very thin support layer to our Daos.

I would approach 'composition or inheritance' choice with a critical mind as there are legitimate cases where each approach fits best.

分享给朋友:
您可能感兴趣的文章:
随机阅读: