• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should it be one DAO per entity?

 
Bala Gangadhar
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ran Pleasant
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic