Over a million developers have joined DZone.

One Big DAO, or One DAO Per Table/Object?

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

For a long time I have been doing DAO's in my applications.I have usually used the model of having one DAO per type persisted, or per database table. You know, a PersonDao, a CarDao, a BlablaDao etc.

Today, as I was writing an application in which I am too lazy to use a DAO layer (because most
of the persistence operations are 1-liners), I was thinking: Should I add that DAO layer, or should
I not care? Well, of course I should add the DAO layer, so SQL statements etc. can be reused,
and modified in a central place, if the database schema changes. Shame on me for being lazy.

But here is my question to you all:

Do you also use one DAO per type persisted?
Or, do you create one BIG DAO which contains all DAO logic?

I am asking, because I feel tempted to go with just one BIG DAO though I have no experiences
with that. Like I said, I usually use one DAO per type persisted. But a BIIIG DAO seems compelling to me...
Here are the immediate benefits I can see:

  1. It definately makes it easier to find all DAO methods in a project.
  2. It makes it very easy to share connections and transactions between different DAO calls.
  3. You don't get confused about whether readCarsForPerson() belongs in the CarDao or PersonDao
    (I would probably say CarDao since it returns Car's).

By using one big DAO the DAO becomes an abstraction of the total database / datastore, rather than an abstraction of each table / type etc.

What is your opinion on this? One big DAO, or one per type? Does anyone have any experiences?

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}