UpTheCreek
UpTheCreek

Reputation: 32401

NHibernate: Avoiding complete in-memory collections when working with child collections via aggregate root

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:

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

Answers (2)

Cole W
Cole W

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

Frederik Gheysels
Frederik Gheysels

Reputation: 56984

I would create a specialized method on your repository, which returns the oldest child of a given parent.

Upvotes: 2

Related Questions