views:

48

answers:

2

Hi - Which of these examples would be the best way to expose a collection of Orders for a specific Person that contain a specific Product and why? Or maybe there is a better way to do this alltogether? (Sorry I am new to domain modeling). The Orders list is pulled from the database with an SQL query and turned into a List collection.

A Person has 1 to many Orders and an Order has 1 to many Products.

1)

class Person
{
    List OrdersContaining(Product p)
    {.....}
}

2)

class Order
{
    List ForPersonContainingProduct(Person person, Product product)
    {.....}
}

2)

class Product
{
    List OrdersFor(Person p)
    {.....}
}

+1  A: 

I would not expose such a method directly on the domain object itself, which encapsulates the data. Rather, I would use the DAO pattern applied to the Order domain. Which, in essence, is a variation of your #2:

class OrderDAO {
    List<Order> listByPersonAndProduct(Person person, Product product){
        .....
    }
}

This way, the various patterns of access that you need to add over time are separated from the Order domain object.

Brandon Smith
Thanks this is very helpful. Can I ask, what would you do in a situation where the relationship is more simple e.g. Person and all associated Orders. Would you model this as another method on the Orders DAO also or as a Person.Orders collection? Person.Orders could call the OrdersDAO method passing in a reference to itself?
A: 

Person could still have a .Orders collection which has all their orders. Then it becomes a question of lazy loading an populating this collection when you know you're going to need it. Something like N/Hibernate helps a lot here.

ShaneC