Announcement Announcement Module
Collapse
No announcement yet.
Referencing other DAOs from a DAO? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Referencing other DAOs from a DAO?

    [Hibernate 3.1.3, Spring 2.0, DB2 V8.2]

    If I have say 20 DAOs that reference each other then wiring in such a configuration seems a bit complex (a setter for every internally referenced DAO). Do most people just (see example below) perform wiring programatically:

    1) Create a local DAOImpl instance
    2) Pass the sessionFactory
    3) Call DAOImpl method

    Example:
    Code:
    UserDAOImpl
    {
       ...
    
       public void setSessionFactory(SessionFactory sessionFactory)
       {
          this.sessionFactory = sessionFactory;
       }
    
       public void saveUser(User user)
       {
         // before saving check project info by passing sessionFactory...
    
         ProjectDAOImpl projectDAO = new ProjectDAOImpl();
         projectDAO.setSessionFactory(this.sessionFactory);
    
         Project myProject = projectDAO.getProjectById(user.myProject().getId());
       }
    }
    
    ProjectDAOImpl
    {
       ...
    
       public void setSessionFactory(SessionFactory sessionFactory)
       {
          this.sessionFactory = sessionFactory;
       }
    
       public Project getProjectById(Long id)
       {
          // Hibernate code
       }
    }

  • #2
    I would recommend using injection on your DAOs and autowiring them in Spring configuration. This is very concise.

    Comment


    • #3
      Clean seperation from Spring

      What about circular references? Say that UserDAOImpl gets ProjectDAOImpl injected and when calling a certain ProjectDAO method an internal reference there is made to a UserDAO method. How is this handled in Spring?

      Comment


      • #4
        I'm not sure whether the circular references can be resolved within the spring context (just try). In any case you are able to set the references using a BeanPostProcessor implementation or an appropriate AutoProxyCreator.

        That said, if I had a design containing interacting DAOs with circular dependencies, I would consider a refactoring to improve the localization of responsibilities in each class.

        Regards,
        Andreas

        Comment


        • #5
          Back again to the original suggestion

          Back to my original suggestion. Why not just handled the problem independently of Spring/AOP? After entering a DAO (that has been succesfully initialied - injected with a SessionFactory) just passing the private SessionFactory to other DAO objects.

          Basically I wondering if there is a major design flaw with my thinking: POJOs supplying either other with session information totally disconnected from Spring except for the entry DAO with gets it's sessionFactory from Spring. Can there be a performance hit? Or drawback to internaling hinding DAO references once one has entered a DAO object?


          Thanks in advance / matthew

          Comment


          • #6
            Well, due to dependency injection your DAOs should be unaware of Spring in either way. Spring just grants you an alternative to programmatically wire your DAOs together by providing means of doing this declaratively. Therefore one way should work as well as the other.

            The benefit of using Spring is, that your DAOs (or beans in general) are independent of each other (given you use interfaces). This enforces a cleaner separation of concerns, leading to a cleaner design and leveraging reuse. If you have a bunch of DAOs which are clinging together you can reuse them only alltogether or none of them.

            Note that I am not advocating to wire _everything_ with Spring. If you factor out private helper classes which are unlikely to be replaced there is no need to provide a means to do so. However, a DAO abstraction should have a restricted responsibility which might indeed be subject to change.

            Regards,
            Andreas

            Comment


            • #7
              Possible danger with circular references

              Alright. Only negative I see is the potential for circular references with singleton objects injected into DAOs. But that can be avoided with a little extra awareness when designing DAOs.

              Thanks for the replies / Matthew

              Comment

              Working...
              X