Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
JSP - A mailing list about Java Server Pages specification and reference
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Subject: Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Subject: Running a Simple JMS Example
Tomcat and webapplication specific java library path
Mapping in workers2 properties
org apache jasper JasperException
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action
   MESSAGE
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
Value attribute of <html:checkbox
url string for connecting jboss to oracle
javax servlet ServletException: BeanUtils populate
5 0 18: Windows XP Pro vs Windows 2000
HTTP Status 404 The requested resource is not available
 
DAO Design

DAO Design

2005-08-20       - By unmesh joshi

 Back
Reply:     1     2     3     4     5     6     7  


Hi,

I have a question regarding design of DAO. In our project we are creating
customer profiles. We have modeled profile like this

class Profile {
 Name name;
 List<Address> addressList;
 List <Phone> phoneList;
 ....
}

We have CRUD operations for Profile, Addresses, and Phones.
For crud operations, we have DAO facade like this

class ProfileDao  {
  void insertProfile(Profile profile);
  void Profile getProfile(String profileId);
  void addAddress(String profileId, Address a);
  void addPhone(String profileId, Phone p);
  void updateAddress(String addressId, Address a);
  void updatePhone(String phoneId, Phone p);
  void deleteAddress(String addressId);
  void deletePhone(String phoneId);
  ..........

}


Client for this code will be like this.

class ProfileClient {
  void testAddAddress() {
Address address = new Address("112 Fifth Street", "NJ", "US", "08857");

String profileId = getProfileIdentifier();

getProfileDao().update(profileId, address);

  }
}

Instead of using Facade like this, I thought we could use it like following.

1. Clients will do all CRUD operations on Profile object itself.
2. All the classes will have isUpdated, isNew and isDeleted flag.
3. Dao  will have just one update(Profile p) method.
4. Dao will check for updates and make queries accordingly

Dao will now become like this.

class ProfileDao  {
  void insertProfile(Profile profile);
  void Profile getProfile(String profileId);
  void update(Profile p);
}

Client for this code will be like this.

class ProfileClient {

  void testAddAddress() {
Address address = new Address("112 Fifth Street", "NJ", "US", "08857");
Profile p = getProfile();
p.addAddress(address);

getProfileDao().update(profile);

  }
}

This second approach looks cleaner, but the concern raised by my team mates is
that every time I need to add, update, delete a single address, I need to load
whole profile object in memory. This looks more like a "stateful"
implementation as opposed to "stateless" implementation of Dao Facade. If
Profile object is a huge one, then it can be a overkill to load the whole
object just to add/change one address. But I certainly dont like the facade
implementation. It does not allow me to do inserts/updates/deletes in batch. I
have to do operations one at a time. Secondly any object level validations or
business rules (Like Profile should have only one delivery address) are harder
to implement in facade approach. What is your opinion on this? Is this a
standard Domain Model vs Transaction Script issue?




__ ____ ____ ____ ____ ____ ____ ____ ____ ____ __
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

====================================================================
Companion Site: http://www.corej2eepatterns.com
J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html
Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected)
<DIV>
<BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER
-LEFT: #1010ff 2px solid">
<P>Hi,</P>
<P>I have a question regarding design of DAO. In our project we are creating
customer profiles. We have modeled profile like this</P>
<P>class Profile {<BR>&nbsp; Name name;<BR>&nbsp; List&lt;Address&gt;
addressList;<BR>&nbsp; List &lt;Phone&gt; phoneList; <BR>&nbsp; ....<BR>}</P>
<P>We have CRUD operations for Profile, Addresses, and Phones.<BR>For crud
operations, we have DAO facade like this</P>
<P>&nbsp;class ProfileDao&nbsp; {<BR>&nbsp;&nbsp; void insertProfile(Profile
profile);<BR>&nbsp;&nbsp; void Profile getProfile(String profileId);<BR>&nbsp;
&nbsp; void addAddress(String profileId, Address a);&nbsp;<BR>&nbsp;&nbsp; void
addPhone(String profileId, Phone p);&nbsp;<BR>&nbsp;&nbsp; void updateAddress
(String addressId, Address a);<BR>&nbsp;&nbsp; void updatePhone(String phoneId,
Phone p);<BR>&nbsp;&nbsp; void deleteAddress(String addressId);<BR>&nbsp;&nbsp;
void deletePhone(String phoneId);&nbsp; &nbsp;<BR>&nbsp;&nbsp; ..........<BR>
&nbsp; &nbsp;<BR>&nbsp;}</P>
<P>&nbsp; <BR>&nbsp;Client for this code will be like this.<BR>&nbsp;<BR>&nbsp
;class ProfileClient {<BR>&nbsp;&nbsp; void testAddAddress() {<BR>&nbsp;Address
address = new Address("112 Fifth Street", "NJ", "US", "08857");</P>
<P>&nbsp;String profileId = getProfileIdentifier();</P>
<P>&nbsp;getProfileDao().update(profileId, address);<BR>&nbsp;&nbsp;<BR>&nbsp;
&nbsp; }&nbsp;<BR>&nbsp;}&nbsp;&nbsp; &nbsp;</P>
<P>&nbsp;Instead of using Facade like this, I thought we could use it like
following.</P>
<P>&nbsp;1. Clients will do all CRUD operations on Profile object itself.<BR>
&nbsp;2. All the classes will have isUpdated, isNew and isDeleted flag.<BR>&nbsp
;3. Dao&nbsp; will have just one update(Profile p) method.<BR>&nbsp;4. Dao will
check for updates and make queries accordingly<BR>&nbsp;<BR>Dao will now become
like this.</P>
<P>class ProfileDao&nbsp; {<BR>&nbsp;&nbsp; void insertProfile(Profile profile)
;<BR>&nbsp;&nbsp; void Profile getProfile(String profileId);<BR>&nbsp;&nbsp;
void update(Profile p);<BR>}</P>
<P>&nbsp;Client for this code will be like this.<BR>&nbsp;<BR>&nbsp;class
ProfileClient {</P>
<P>&nbsp;&nbsp; void testAddAddress() {<BR>&nbsp;Address address = new Address(
"112 Fifth Street", "NJ", "US", "08857");<BR>&nbsp;Profile p = getProfile();<BR>
&nbsp;p.addAddress(address);</P>
<P>&nbsp;getProfileDao().update(profile);<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp; }
&nbsp;<BR>&nbsp;}&nbsp;&nbsp; &nbsp;</P>
<P>This second approach looks cleaner, but the concern raised by my team mates
is that every time I need to add, update, delete a single address, I need to
load whole profile object in memory. This looks more like a "stateful"
implementation as opposed to "stateless" implementation of Dao Facade. If
Profile object is a huge one, then it can be a overkill to load the whole
object just to add/change one address. But I certainly dont like the facade
implementation. It does not allow me to do inserts/updates/deletes in batch. I
have to do operations one at a time. Secondly any object level validations or
business rules (Like Profile should have only one delivery address) are harder
to implement in facade approach. What is your opinion on this? Is this a
standard Domain Model vs Transaction Script issue?</P>
<P>&nbsp;</P></BLOCKQUOTE></DIV><p>__ ____ ____ ____ ____ ____ ____ ____ ______
__ ___<br>Do You Yahoo!?<br>Tired of spam?  Yahoo! Mail has the best spam
protection around <br>http://mail.yahoo.com
====================================================================
Companion Site: http://www.corej2eepatterns.com
J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html
Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.