开发者

Is this a place for ActiveRecordMediator<T>? Knowing I shouldn't do child fetches ahead of time

开发者 https://www.devze.com 2022-12-16 00:53 出处:网络
In a hierarchical data model i have Parent and Child.Parent has Fields[] and each child will al开发者_Go百科so have the same \"Count\" of fields.. we\'ll call them ChildField[]

In a hierarchical data model i have Parent and Child. Parent has Fields[] and each child will al开发者_Go百科so 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?


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:

  • http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx
  • http://www.castleproject.org/activerecord/documentation/trunk/advanced/tuning.html

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:

  • http://www.udidahan.com/2007/04/23/fetching-strategy-design/
  • http://www.udidahan.com/2007/09/16/fetching-strategy-nhibernate-implementation-available/
  • http://ayende.com/Blog/archive/2008/03/27/Adaptive-Domain-Models-with-Rhino-Commons.aspx
0

精彩评论

暂无评论...
验证码 换一张
取 消