Is it posible to build a common DAO class for different beans/tables?
Currently I am creating different DAO classes for each different table like: CustomerDAO
, EmployeeDAO
, StudentDAO
.
Asked
Active
Viewed 632 times
0
-
Yes you can do that. – Darshan Lila Aug 25 '14 at 13:16
-
Yes you can. But having different DAO makes them more focused and specific in my opinion and will make your code readable and easy to maintain if it grows. – Omoro Aug 25 '14 at 13:27
1 Answers
0
Yes it is possible.
How ?
This is Base DBO class for all your CustomerDBO, EmployeeDBO, StudentDBO.
public class BaseDBO {
}
Now, each of your DBO will be like this,
public class CustomerDBO extends BaseDBO {}
public class EmployeeDBO extends BaseDBO {}
public class StudentDBO extends BaseDBO {}
Write all the common methods in BaseDAO like
public class BaseDAO<T extends BaseDBO> {
void create(T t){}
void update(T t){}
void delete(T t){}
}
Now on specific DAO looks like
CustomerDAO,
public class CustomerDAO extends BaseDAO<CustomerDBO> {
}
Now, for CustomerDAO contains all the methods which are common for DAO.
Similarly for StudentDAO, EmployeeDAO.
Hope this helps !!

Vinay Veluri
- 6,671
- 5
- 32
- 56
-
You actually don't need to have your entities (what you call DBO here) extends a `BaseDBO`. You can have your `BaseDAO`just take `T`. `public class BaseDAO
` – Matt Aug 25 '14 at 14:07 -
@Matt, Funda is, If I have common columns like id/creation time, I can use this Base class to define them - not to make DBO to look clumsy. For this particular scenario to define the common DAO, it is not required - I agree to that point. – Vinay Veluri Aug 26 '14 at 04:54