Reputation: 949
In our project, we use DAO layer to access data from the database, which simply queries the database using PreparedStatement
and returns the ResultSet
to the service layer. I know this is a bad idea to return ResultSet
to the service layer, as many complications arise while closing the ResultSet
, PreparedStatement
, Connection
efficiently. Again the best idea would be to populate the data to a DTO from DAO layer and access the data from DTO in the service layer, but our code is written such that there is only one generic method for querying data from the database and it returns the ResultSe
t. This method is called from several places of the service layer and we cannot change all the places and write DTO there.
So my question is what would be the best way to return data from DAO to service layer in this case? Any suggestion would be good.
Upvotes: 0
Views: 372
Reputation: 159096
The entire point of the DAO is for it to abstract Data Access away from the service layer, i.e. the service layer should not know (or care) if the data comes from a:
As such, it is the DAOs job of converting the SQL results into a (list of) DTO(s).
If you have a single generic class for querying the database, and it just returns a ResultSet
, then that class is not a DAO. It is a helper class to be used by the DAO classes.
Seems you're actually missing the Data Access layer, so to answer your question:
What would be the best way to return data from DAO to service layer?
Write actual DAO classes that return DTOs.
Upvotes: 1