Reputation: 4003
Could it be said that when you reach the point of injecting one DAO into another one, you've already gone over the DAO scope, and reached a business layer issue?
NOTE: I am not having a particular issue in mind, but just trying to extract a general rule of thumb regarding the use of DAOs.
Upvotes: 1
Views: 143
Reputation: 120761
May you should start with thinking of what a DAO is.
If you use JPA, then the entiy manager is already a generic DAO (by DAO Pattern). What the most Java EE developer call a DAO is not a DAO by DAO Pattern. It is some kind of refactoring: move out the Database related statements into an external class (and I think that is the kind of DAO you are talking about) Don't get me wrong, I think this is usefull.
So my understanding of this DAO is some refactoring thing. And the overall goal of refactoring is making the code more readable, maintainable. So if you code becomes better with this indirection then go on, but you should document that your project DAO-Pattern is slightly different from the DAO Pattern used by may other Java EE Developers.
Upvotes: 0
Reputation: 6742
Yes. DAOs shouldn't be dependent on each other.
It's the job of the business / service layer to coordinate different DAOs.
However, if you'd describe your specific scenario we would be able to give a more accurate answer.
Edit:
After reading @edutesoy's answer, I see the logic in his argument.
So I'll refine my answer by saying- it's not inherently wrong to do that, but it is a little 'smelly'.
This is because of the normal structure of your DAO layer- you usually have a DAO for each type of entity (CustomerDAO
, OrdersDAO
etc) . If your CustomerDAO
is using your PaymentsDAO
, it smells a bit like a violation of SRP: is CustomerDAO
responsible also for payments-related operations?
So, in conclusion- I would definitely require a very good reason for this before introducing it into my code.
Upvotes: 1
Reputation: 10273
The division between DAO and Business is "arbitrary". We say that a class is a DAO when it is used to "retrieve and store data from storage repositories". The fact that you inject a DAO in another DAO doesn't prevent it to "retrieve and store data from storage repositories", so for me the answer to your question is NO.
There is nowhere said that you cannot inject a DAO into another (even though it is not usually done).
Upvotes: 3