Monday, February 12, 2007

DAO Best Practices

I am currently working on a Flex 2 tutorial using AmfPhp 1.9 that I should have done by the end of the week :) While I was writing the DAO classes in PHP I stumbled upon a design issue. What should my CRUD methods return. They could return a boolean value that indicated that the methods completed successfully or I could return the new/updated value object. My example application uses a datagrid bound to an ArrayCollection of my VO's. Now upon successful completion of my CRUD method do I refresh the whole grid or just the VO that was changed? I am strongly leaning towards returning the VO. Any thoughts.


Anonymous said...

Hey man. I am looking forward to that AMFPHP 1.9 tutorial.
I believe the return value should be True if successful and False if there was an error. Or maybe you can do 0 for True and 1 thru 9 for the number of different errors that can come back as to why the SQL statement failed. Of course this should be for those statements that are not SELECT. For those as usual the record(s) should be returned.

Lots of luck,

xv15mods said...

the only problem is you might have to call ArrayCollectionNameHere.refresh() after you update the VO in order for the datagrid to display any information that might not have updated properly.

its far easier to replace the full ArrayCollection.. but in my opinion a better practice to update only what has changed.

Derek Wischusen said...

I prefer to return the VO and just update the individual object. There is a decent amount of code that gets executed when you update a collection. So I think it is more efficient just to update the object.

Mike C. said...

I included these in my VO:
private function Success($id=0,$message='')
return array(
'status' => 0,
'id' => $id,
'result' => $message

private function Failure($status=-1,$message='')
return array(
'status' => $status,
'id' => 0,
'result' => $message

For instance, I return:
return $this->Failure(-1,"Invalid Login");

return $this->Success(0,$currentUser);

Mantasha said...

Great site buddy . Really I like ur site I think u r also a Java Developer like me any ways I have some reference books of java which are readable .... Following are the books

Programmer's Guide to Java
Certification A Comp. Primer SE
By Khalid A. Mughal, Rolf W. Rasmussen Publisher : Addison Wesley
Pub Date : August 04, 2003

Java™ Development on PDAs
Building Applications PocketPC
By Daryl Wilding-McBride Publisher : Addison Wesley
Pub Date : June 05, 2003

Learning Java™, 2nd Edition
Publisher : O'Reilly Pub Date : July 2002
Pub Date : June 05, 2003

Jython for Java Programmers
By Robert W. Bill Publisher : New Riders Publishing
Pub Date : December 21, 2001

Enterprise JavaBeans, 3rd Edition
By Richard Monson-Haefel Publisher : O'Reilly
Pub Date : September 2001

Java 1.5 Tiger: A Developer's Notebook
By David Flanagan, Brett McLaughlin Publisher : O'Reilly
Pub Date : June 2004

Java Tutorials Index 1
Java Data Objects
By David Jordan, Craig Russell Publisher : O'Reilly
Pub Date : April 2003

Java™ Extreme Programming Cookbook
By Eric M. Burke, Brian M. Coyner Publisher : O'Reilly
Pub Date : March 2003

Java™ Performance Tuning, 2nd Edition
By Jack Shirazi Publisher : O'Reilly
Pub Date : January 2003

Java™ Performance Tuning, 2nd Edition
By Jack Shirazi Publisher : O'Reilly
Pub Date : January 2003

JavaScript & DHTML Cookbook
By Danny Goodman Publisher : O'Reilly
Pub Date : April 2003

Java Servlet & JSP Cookbook
By Bruce W. Perry Publisher : O'Reilly
Pub Date : January 2004
Java Tutorials Index 2