I just want to explain it in my own way with a small story that I experienced in one of my projects. First I want to explain Why DAO is important? rather than go to What is DAO? for better understanding.
Why DAO is important?
In my one project of my project, I used Client.class
which contains all the basic information of our system users. Where I need client then every time I need to do an ugly query where it is needed. Then I felt that decreases the readability and made a lot of redundant boilerplate code.
Then one of my senior developers introduced a QueryUtils.class
where all queries are added using public static
access modifier and then I don't need to do query everywhere. Suppose when I needed activated clients then I just call -
QueryUtils.findAllActivatedClients();
In this way, I made some optimizations of my code.
But there was another problem !!!
I felt that the QueryUtils.class was growing very highly. 100+ methods were included in that class which was also very cumbersome to read and use. Because this class contains other queries of another domain models ( For example- products, categories locations, etc ).
Then the superhero Mr. CTO introduced a new solution named DAO which solved the problem finally. I felt DAO is very domain-specific. For example, he created a DAO called ClientDAO.class
where all Client.class
related queries are found which seems very easy for me to use and maintain. The giant QueryUtils.class
was broken down into many other domain-specific DAO for example - ProductsDAO.class
, CategoriesDAO.class
, etc which made the code more Readable, more Maintainable, more Decoupled.
What is DAO?
It is an object or interface, which made an easy way to access data from the database without writing complex and ugly queries every time in a reusable way.