views:

55

answers:

1

Hi there,

I'm using NHibernate, and have a one to may mapping set up. Let's call it Customer -> Orders This all works, and I can get all the orders for the customer.

Now, I'd like to filter these orders, let's say I just want orders between run time determined dates.

public class Customer
{
    ...

    // mapped from NHibernate mapping file
    public IList<Orders>Orders {get; set; }

    // get a filtered list of orders
    public IList<Orders>GetOrders(DateTime start, DateTime end) { ... }

    ...
}

So I could Enumerate over all of the orders and select the ones I want, but I'd like to defer the filtering off to the database as the number of orders could be very large.

The question is, can I do this in the mapping file with a filter? Or do I need to create a method on a repository to do this and access that method from my domain object?

Any other suggestions are also welcome....

Many thanks

RR

A: 

Does your Customer truly make sense as an aggregate root of an Order? This is a decision only you can make but it's probably not path I would take. Instead I'd consider Order as its own aggregate root with a link to a customer since I imagine quite a few screens where you care about Orders directly.

If an Order becomes an aggregate root then it gains it's own repository and the ability to be directly queried against. At that point you just write a query which fulfills your needs.

ShaneC
Thanks for the answer Shane, but I need to think of this more generically. Specifically, If I need to get a filtered list of child objects in any domain what is best practice? Take a hotel room. It will have 365 'availability' objects for a year, but I only want the objects for say a week. Loop through in code? or how to do this via the database with Nhibernate/Repository pattern.
RR
Actually now that I look at your question it seems more like you may possibly be breaking Command Query Seperation (CQS). Commands such as "Reserve a hotel room with these parameters" should be applied against your domain model (don't forget "tell.. don't ask") but there should be another mechanism for Queries such as "Show me all available rooms".
ShaneC