tags:

views:

45

answers:

2

I'm using the (slightly dated) links below as a guide to learning and understanding proper use of DAOs in my web apps.

http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html

http://balusc.blogspot.com/2008/07/dao-tutorial-use-in-jspservlet.html

Generally speaking, is it correct to initialize a DAO in the INIT() method of a Servlet, and then pass that DAO to the model ? Or should the DAO be more tightly integrated in the Model, or perhaps a part of the Model ? That is... is there a better way for the Model to acquire a tailored DAO ?

Admittedly I'm stretching my knowledge of the subject here, so if I've worded this poorly I apologize. Enlightenment is always welcome.

+1  A: 

I'm not from the Java world, so probably it's not my opinion you're seeking for. But I'm pretty sure that the ideal way to do it is to have your DAOs loosely coupled to the rest of your Model (your probably talking about DAOs X business classes - to me both DAOs and business objects belong to "The Model", but your mileage may vary).

That way it's easy to replace all your DAO layer. I have created an application with 2 distinct and complete DAO implementations: one using Oracle DMBS, the other MySQL. Maybe that's what you're looking for - and it's easy using the abstract factory pattern.

Of course, Java programmers can use the Spring IoC container for that, I suppose :)

rsenna
Thanks guys, I'll look in to this.
JHarnach
A: 

Spring implements what is called the factory pattern. This lets spring handle creating the DAO objects that are used in your application. When you do this, the implementations are very loosely coupled with the code calling them.

Using Spring (or a similar tool) is much better than creating instances of your DAO's in classes that call them.

Here is a tutorial that talks about using Spring with DAO's:

http://www.ibm.com/developerworks/java/tutorials/j-spring2/

Jay