View Full Version : Is there any difference between DAO and IOC?
liren
Sep 14th, 2004, 01:31 AM
Hi everyone:
I am new to Spring . There is an problem puzzled me some time.
I use DAO pattern in this way before:
ITradeDAO tradedao=DAOFactory.getInstance().getTradeDAOInsta nce();
// use the tradedao here
........................
.......................
But in Spring I use IOC in this way:
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! :(
Alef Arendsen
Sep 14th, 2004, 09:12 AM
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
liren
Sep 14th, 2004, 06:34 PM
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
Alef Arendsen
Sep 15th, 2004, 04:03 AM
You're using a DAO, but you're creating it using an absract factory pattern. So in fact you're using two patterns ;-)
liren
Sep 15th, 2004, 05:05 AM
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.
:)
Keith Donald
Sep 15th, 2004, 08:06 AM
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.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.