Announcement Announcement Module
Collapse
No announcement yet.
Is there any difference between DAO and IOC? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there any difference between DAO and IOC?

    Hi everyone:

    I am new to Spring . There is an problem puzzled me some time.
    I use DAO pattern in this way before:
    Code:
    ITradeDAO tradedao=DAOFactory.getInstance().getTradeDAOInstance();
    // use the tradedao here
    ........................
    .......................
    But in Spring I use IOC in this way:
    Code:
    ITradeDAO tradedao;
    public void setTradeDAO(ITradeDAO tdao){
        this.tradedao=tdao;
    }
    // use the tradedao here
    .....................................
    Is there any difference between them? I mean that is there any benefit if I use Ioc? Is it more benefit than DAO design pattern?
    Could some friends tell me that difference? Thks!

  • #2
    What you've been using before isn't exactly the DAO pattern. You're creating DAO, but using the Abstract Factory pattern.

    IoC replaces, no implements and combined creational patterns, such as the one mentioned above and others like the Builder pattern and the Prototype.

    Of course the result of your two code snippets is exactly the same, except for the fact that the one using the programmatic Abstract Factory pattern is referencing the factory itself. It knows where to get the DAO whereas in IoC, it wouldn't. When using Dependency Injection (you better use that term) the DAO is injected by the framework, without passing the dependant information about where it got the DAO.

    Read Martin Fowler's piece on DI: http://martinfowler.com/articles/injection.html

    Comment


    • #3


      Originally posted by Alef Arendsen
      What you've been using before isn't exactly the DAO pattern. You're creating DAO, but using the Abstract Factory pattern.

      Read Martin Fowler's piece on DI: http://martinfowler.com/articles/injection.html
      Thank you for your reply.I understand it now. :-) You mean that my first code is not DAO pattern? I see it in the "Sun's blueprints " .So mybe I think wrong . I also want to know how to implements it using DAO . Could you change above code to a DAO implement? Thks

      Comment


      • #4
        You're using a DAO, but you're creating it using an absract factory pattern. So in fact you're using two patterns ;-)

        Comment


        • #5
          :-)

          Originally posted by Alef Arendsen
          You're using a DAO, but you're creating it using an absract factory pattern. So in fact you're using two patterns ;-)
          Thank you. I know it now.

          Comment


          • #6
            You're still using the DAO pattern here in both cases in that you have a logical data access object whose interface defines a contract meeting your persistence requirements.

            What's different is how the DAO is located. In the getInstance() case you're going out and finding it, in j2ee speak this is called a "Service Locator" and you're locating it using a Singleton.

            In the IoC an assembler object -- the spring container -- locates the DAO for you and hands it to you - the user of the DAO is removed from knowing where the DAO reference came from. The main advantage is you make service location a 'separate concern' allowing you to vary it more easily, and use your DAO client more easily in different envrionments.

            Comment

            Working...
            X