views:

24

answers:

1

In a hierarchical data model i have Parent and Child. Parent has Fields[] and each child will also have the same "Count" of fields.. we'll call them ChildField[]

public class Parent : ActiveRecordBase<Parent>
{
   [HasMany]
   IList<Field> Fields {get; set;}
   [HasMany]
   IList<Child> Children {get; set;}
}

public class Child : ActiveRecordBase<Child>
{
   [HasMany]
   IList<ChildField> ChildFields {get; set;}
}

Now when you don't have any Fields inside Parent... but i have say 500 Child objects in its collection I sure dont want 500 selects (total) where each child tries to load its childField collection.

I suppose I can maybe do Lazy but at the same time.. when there are childFields I don't want it lazy and eager is going to be more performant isn't it?

ActiveRecordMediator might allow me to go something like

public void FetchByChild(Child child){    
child.Parent.Fields.Count > 0
       //Do the fetch
    else
       //return;
}

Does the mediator serve the purpose to "skip" db calls when additional knowledge of the "calling-context" is provided?

+1  A: 

The question is a bit confusing... ActiveRecordMediator is only a NHibernate ISession accessor. It can't skip queries or anything like that. It only executes the queries that you feed it.

In NHibernate (and ActiveRecord of course) you can use lazy or eager fetching on a per-query basis. That is, if for a particular query you know you'll need those Child entities you can fetch them eagerly. If you don't, let them be lazy.

These articles are a great introduction to lazy/eager loading:

There are also other perf tweaks you can do, like batch fetching for collections

If you meant setting your fetch strategy from an external context, see these articles:

Mauricio Scheffer