lazy saunter

(personal learnings in computation)

Wednesday, September 28, 2011

data access objects, design pattern for java applications

at my new job, i inherited for maintenance the a-fore-mentioned struts 1 web application. whoever originally wrote it followed a design pattern for separating the application logic from the database/persistence logic that revolves around the use of data access objects, DAOs.

apparently the history of DAOs goes back to the 1994 book "Design Patterns". the four authors are referred to as the "gang of four" and often references to one of the 23 design patterns they described are suffixed with "GoF" (i never knew what this meant until today). their book treated object-oriented programming as a whole, in fact, including examples from C++ rather than java which must have been very new at that time.

the idea is simple: the main programmatic logic and code of an application should not include low-level database access code. given changes to the underlying database, this code should not need to be re-written. instead, it should utilize a DAO class and the methods available in the DAO class. these should include basic Create, Read, Update, and Delete operations pertaining to a specific object. in a simple case, the DAO may provide methods for CRUD operations for something like an Employee object. in the DAO code itself, the actual SQL statements required to perform these operations on the Employee database table, as well as the code to execute and/or return the result, are located. often the result is returned in the form of an object of the type in question, in the example, as an Employee object. while the Employee class follows the regular java-bean convention and is a POJO (plain old java object), in this paradigm, it is referred to as a data transfer object, DTO.

literature describing DAO implementation in java mostly dates to the time around 2003 when, apparently, their use was in full swing. currently, in 2011, building applications around this paradigm is considered rather outdated. nevertheless, if you have never coded using DAOs like myself (though i have typically coded php and perl to hold database specific functions in separate files, i rarely created files only for dealing with a single object as i was not using an OO set-up), the fact that it puts a name on a method for accomplishing something that every programmer has probably developed his/her own system for accomplishing is good in that it makes the code predictable, readable, and maintainable. i have seen other programmers' code that uses no system for this task and it really made me dread having to program java web apps in the future.

one criticism of DAO code is that it requires a lot of extra coding up front to achieve separation between the application layer and the persistence layer that aids in maintenance but may not even be necessary. another observation is that the DAO code still contains database-specific SQL. fortunately, both of these shortcomings would be resolved in the following years with tools like hibernate which is what is being widely used today.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home