• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Domain class design question

 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class called Order and a class called LineItem.





Now this is fine if I am retrieving from the db the entire order with all the lineItems. But most of the time I am retrieving just one lineItem. I still need the order info too. I have thought of doing this:



This seems sloppy using one member currentLineItem and a List lineItems together. I have also thought of doing this to retrieve the current lineItem from order:

LineItem lineItem = order.getLineItems().get(0);

This seems sloppy too, since I can never really be sure that the List will only have one LineItem in it without checking and it is the one I retrieved. I like the first example better, but it still doesn't seem right. What would be the best way of representing this Order class given that most of the time I am only needing to retrieve one Order with one lineItem?

Thanks,

Warren
[edit]Please use the code button; I have edited this post so you can see how much better it is to read. CR[/edit]
 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got this post in the wrong forum. It should be in Intermediate.

Warren
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

You simply need to check your list of line items first:

Is it empty? If it's not empty then grab the 1st line item as the current line item (Note, you're assuming that the first item in the list is the current line item, but are you guaranteeing this definition/order?) as you stated above ...get.(0)..
 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this considered good design? It just seems like there is too many checks that would have to be made.

isEmpty() == false

size() ==1

plus I would have to make sure the List was empty every time I retrieved a new LineItem.
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two checks would not be considered bad design though. But do think about how you can have the actual code for that only in one place.

Why would you need to check if the list was empty every time you get a line item?
 
Rosco Duncan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered adding an Order property to the LineItem?

This would be the inverse relationship to the List of LineItems that the Order has. The idea being that if you were to get a handle to an individual LineItem, you would then have the reference back to its Order.

Are you doing some sort of Object Relational mapping to load these from the database?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic