It's not a secret anymore!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Should it be one DAO per entity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Should it be one DAO per entity?" Watch "Should it be one DAO per entity?" New topic
Author

Should it be one DAO per entity?

Bala Gangadhar
Ranch Hand

Joined: Oct 07, 2008
Posts: 82
    
    1
Hi,
In my project the persistance layer is implemented in such a way that one DAO per service. For example GenerateCSVDao for csv generation service and so. This DAO may access more than one table and may handle more than one entity.
Now i was asked to refactor the dao layer to make use of Generics. When I gone though the hibernate site for DAO patterns using Generics, DAO's declaration is like CustomerHibernateDao<Customer, custId>.

My doubt is Generic DAO pattern applicable only when one DAO per entitiy cases?
Can't i make use of Generics in my project? if so how?

Thanks in advance,
Bala.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30382
    
150

Bala,
DAOs can be, but don't have to be, one per entity.

The person who asked you to use generics might have meant on a method level rather than a class level. For example, DAOs frequently query for a list of something. You could return List<Customer> rather than List.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 75
Bala

In many cases each entity needs its own DAO. However, when possible have your DAO objects deal with components of objects. For example, you want a DAO for an Invoice class but you probably don't want a DAO for the related LineItem class. Deal with the LineItem objects only as part of Invoice obect itself, ie save the LineItems objects by saving the Invoice object. If you have a DAO for every entity you are probably dealing with an Anemic Domain Model.


Ran
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Should it be one DAO per entity?