Reputation: 32401
Considering the simplified class below, lets assume:
class Parent { public IList<Child> Children { get; set; } public Child OldestChild { get { return Children.OrderByDescending(c => c.Age).FirstOrDefault(); } }
When using NHibernate and Repositories, is there some best practice approach to satisfy both:
a) Should select oldest child through the aggregate root (Parent) - [ie. without querying the children table independently via e.g. a ChildRepository using the Parent Id]
b) Should avoid loading the whole child collection into memory (ideally the oldest child query should be processed by the DB)
This seems something that should be both possible and easy, but I don't see an obvious way of achieving it. Probably I am missing something?
I'm using NHibernate 2.1, so a solution for that would be great, although will be upgrading to 3 soon.
Upvotes: 4
Views: 272
Reputation: 15313
You could map OldestChild using a Formula. Take a look at this to map a class to a formula: http://blog.khedan.com/2009/01/eager-loading-from-formula-in.html
Upvotes: 2
Reputation: 56984
I would create a specialized method on your repository, which returns the oldest child of a given parent.
Upvotes: 2