Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 27th, 2011, 03:42 PM
peenie's Avatar
peenie peenie is offline
Google Relay Server
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Oh christ I don't even know any more.
Posts: 1,812 peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)  Folding Points: 9953 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 1 Day 19 h 12 m 24 sec
Reputation Power: 437
Send a message via AIM to peenie Send a message via MSN to peenie
Separating business logic, DAO, entities.

I'm an experienced programmer but I'm inexperienced in database application development. I have a relatively straightforward application I'm developing (in Java, using Hibernate for data access) and I have one question about best practices concerning the separation of business logic and database access.

Two of the objects I'm working with are Groups and Items. A Group can have many Items, an Item can only be in one Group.

I have two basic layers so far:

First, I have my entity classes, which are Group and Item bean classes. These each have their respective properties. Item contains a reference to the Group it is in, Group contains a set of the Items in it. The Group's Item set is populated by Hibernate. Item contains a foreign key from the Group table. Hibernate takes care of cascading database updates/inserts/deletes so I only need to explicitly commit new Groups - new Items are managed automatically.

Second, I have my DAO's. They are run-of-the-mill DAO objects with basic methods to look up objects by ID, and add/remove objects in the database.

My question is general but I will give a specific scenario. Given a Group, I want to find all Items in that group whose name contains a certain string.

I have three ways of doing this:

1. I can add a method to my ItemDAO to retrieve a list of items given a Group and a name string. Then, in my application, I would do this (pseudo-ish code):
Code:
List<Item> items = itemDAO.findByGroupAndName(theGroup, theNameString);

Advantages: Simplicity
Disadvantages: DAO calls in application; DAO instance must be available (they aren't static/singletons, I use factories to access).

2. I can do #1, but then add a method to my Group entity class that retrieves the list of items in that group using the ItemDAO. Then, in my application, I would not make DAO calls to find the items, I would do:
Code:
// in the group:
return getItemDAO().findByGroupAndName(this, theNameString);

// elsewhere, in the application:
List<Item> items = theGroup.findItemsByName(theNameString);

Advantages: Item finding is a method of Group. Application does not call DAO.
Disadvantages: DAO calls from entity layer. DAO instance must be available to Group entity.

3. I can add a method to Group that searches the Group's Item set for matching Items, plain-old-Java style.

Advantages: Item finding is a method of group. Item finding logic does not involve any DAO or data-access specific calls. Works with Groups that are not persistent. Logic is independent of database - entity classes do not depend on DAO.
Disadvantages: All Items must be loaded from database for persistent group (this happens automatically, lazily, managed by Hibernate). In this small application this is unlikely to be an issue. In larger applications, I don't know.


What is the best option here in terms of not digging myself into a hole down the road, and keeping my code maintainable, extendable, and reusable?

I don't know if the advantages and disadvantages I listed above are actually advantages and disadvantages, so put a question mark after all of those.

Thanks!
-J

Edit: Removed some contributions that my cat made to this post while I wasn't looking.
__________________
OMG RAVER CHICKS!!
On a related note: C/C++ Programming Tutorials


"Science is based on reality staying the same, and Nature ignores what humans vote upon." -- Bill Beaty
"Three litres of sherry up the butt can only be described as astounding." -- Darwin Awards

Last edited by peenie : November 27th, 2011 at 03:55 PM.

Reply With Quote
  #2  
Old November 30th, 2011, 06:03 AM
raytechpro. raytechpro. is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 37 raytechpro. User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 46 m 25 sec
Reputation Power: 4
I don't know java but it is normal for business objects to have access to the DAO. Therefore I would go for option 2.

Option 1 would be a no as you're accessing the DAO from the GUI level.

Don't like the idea of 3.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Separating business logic, DAO, entities.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap