Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros and cons of the use of the DAO pattern [closed]

As I mention in the title, I'm interested to know what you (as experienced developers) think about the use of the DAO pattern, specifically within a web application. What advantages have you found and what consequences of its use have you disliked?

like image 272
Sheldon Avatar asked Nov 17 '09 11:11

Sheldon


People also ask

What is the purpose of DAO pattern?

The Data Access Object (DAO) pattern is a structural pattern that allows us to isolate the application/business layer from the persistence layer (usually a relational database but could be any other persistence mechanism) using an abstract API.

How does DAO pattern abstract away the details?

The Data Access Object ( DAO ) pattern abstract away the details of persistence in an application. Instead of having the domain logic communicate directly with the database, file system, web service, or any other persistence mechanism : The domain logic speaks to a DAO layer instead.

What is the difference between DAO and Repository patterns?

DAO is an abstraction of data persistence. However, a repository is an abstraction of a collection of objects. DAO is a lower-level concept, closer to the storage systems. However, Repository is a higher-level concept, closer to the Domain objects.


1 Answers

The problems with DAOs that I have seen is that they typically handle full objects all the time. This creates completely unneeded overhead that wouldn't exist with simple queries. For example, if a drop down is to be created off of database reference data, a DAO user may simply say: "Get me the collection of objects for this table where y ordered by z". Then, that data is used in the dropdown, but typically only for a key/value combination, ignoring everything else in the objects (created data, last user who updated it, whether or not it is active, etc) that was retrieved and mapped. Even if this massaging happens near the DAO call and the objects do not get stored as they are retrieved (which is typically not the case, unfortunately, the objects are often wrapped in a c:forEach (JSP) and iterated over to produce a drop down), it still creates unneeded database and network overhead, not to mention the temporary increase in memory to hold these objects.

Now, this is not to say that a DAO can't be designed to retrieve a Map of reference data - it certainly can. But typically they're used for the full object mapping, which is not what is needed all the time. It is a strength when saving, but a weakness, IMO, when retrieving data - sure, you get all of it - but often you don't need all of it, and it just wastes memory, bandwidth and time.

like image 156
MetroidFan2002 Avatar answered Sep 19 '22 03:09

MetroidFan2002