Discussions

EJB design: DAO: is it ok to have a DAO depending on another?

  1. Hi there! Is it ok to have one of my DAOs depending on another one? This happens because of a composition in my Tables.

    I have a DAO for users and one for Projects, but projects have users so I have something like this:

    public class userDAO{

    findUser(id)
    }


    public class projectDAO{

    findProject(id){
    project.addUser(userDao.find(userID));

    }


    is it ok, or am I breaking some pattern rule here?

    Threaded Messages (4)

  2. Why Not[ Go to top ]

    Why not

    public class projectDAO{

    findProject(id,<whatever_userDAO_returns>){
      call to projectDAO
    }

    ?
  3. Why Not[ Go to top ]

    Cuz I don't wanna bother my clients to know about the other dao (although some other clients use it) That's why, I'd like to make it clear for the client. It only calls one DAO, the DAO it self resolves it dependencies.
  4. Manager?[ Go to top ]

    Cuz I don't wanna bother my clients to know about the other dao (although some other clients use it) That's why, I'd like to make it clear for the client. It only calls one DAO, the DAO it self resolves it dependencies.
    What I've done in a situation like this is use a manager to serve as the outtermost point of contact. In your case, I guess you would have a Project manager which would contain references to the ProjectDAO & UserDAO. With Spring you can just inject the different DAO's that are required by and let the manager handle any inter-dao business logic. Your saveProject() logic in the Manager could do the neccessary operations on the project dao and then do operations using the user dao.

    I like to treat my DAO's as trivial db interactions, add remove update query, and let the managers build on the exposed functionality.
  5. Hi,

    In my experience it is best _not_ to let DAOs depend on each other.

    For instance, consider the case of integration between different databases, where different DAOs are for different databases. If you mask the DAO from the client application then you have to implement all referential integrity rules in the DAO itself.
    But the referential integrity can be seen as business logic, not so much database-specific. By implementing it outside the DAO, you can reuse the same logic across different databases and DAOs.

    Another reason is the complexity of your value objects. If you mix DAO implementations with one another then it is also easy to end up with many dependencies between your value object classes. In my experience this leads to messy design and code.

    Hope that helps,
    Guy
    Java Transaction Processing Software
    Hi there! Is it ok to have one of my DAOs depending on another one? This happens because of a composition in my Tables.I have a DAO for users and one for Projects, but projects have users so I have something like this:public class userDAO{findUser(id)}public class projectDAO{findProject(id){project.addUser(userDao.find(userID));}is it ok, or am I breaking some pattern rule here?