Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Design: should I add a 'DAO' layer between my Session bean and my Entities?

 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new to EJB3, and would like to ask a design question:

Is it customary to add a 'Data Access' layer between the Session beans and the Entities?
Or would your Session bean simply access the Entity API directly?

For example, say I have a 'Bank' session bean, and Account entities.

Then Bank can directly access the Entity API:


But I'm considering an alternative of writing a Data Access layer, something like the following.
It seems more work, but the session bean becomes more readable (especially if it has some additional complex logic and not just simple 'find'), and it can hide some sql implementation choices (e.g. if we find some way to optimize the query, or move to native sql, etc):


Of course this is a simplified example... in real life, the code may be more complex (especially the Session logic), and a query may be re-used from several methods.

If anyone can share their views, and/or refer me to EBJ design tutorials, it will be appreciated.
Thanks
[ September 24, 2007: Message edited by: Sol Ma-Orn ]
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sol,

I would use a DAOFactory in your Session Bean. See the links I gave in this thread why DAO? from JavaRanch's ORM forum for more info.

Regards,
Darya
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the excellent reply and the link.

Can I just ask: how do you make the DAO available to the Session bean?
I'm considering 3 alternatives (see below).
Which seems most reasonable, in your experience ? Or do you have a different, better approach?
Thanks again.

Options I was considering:





[ September 26, 2007: Message edited by: Sol Mayer-Orn ]
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about a factory which returns the DAO instance. The factory could be configured to return the correct instance depending on a configuration which is set before the EJB container starts up.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Roger Chung-Wee, for this reply.
The factory approach sounds good - neat and flexible.

But in that case, could I just shift my question a little: how do you make the *Factory* available to the Session bean ? Is your factory just a static method (or static singleton) ? Or is your factory a Local session bean ? etc.

Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic