How to know if ActiveRecord Lazy Loading is really on ?

Let’s say we have an entity named “Blog” that has many “Post”s in it:


[ActiveRecord(“Blogs”)]
public class Blog : ActiveRecordValidationBase<Blog>
{
   private IList _posts;
   // … snipped … 

   [HasMany(typeof (Post), 
      Table = “Posts”
      Lazy = true,
      ColumnKey = “BlogId”)]
   public IList Posts
   {
      get { return _posts; }
      set { _posts = value; }
   }
   
   // … snipped …
}


Configure your web application to support lazy loading:



  1. Use the isWeb=”true” attribute in your web.config file – look here for “how to”.

  2. Open a SessionScope at the beginning of each request – look here for “how to”.


Now, How do you know if lazy loading is (really)enabled:


Blog b = Blog.Find(1);
bool isInit = NHibernateUtil.IsInitialized(b.Posts);


If isInit shows true, then lazy loading wasn’t enabled and you need to make sure you didn’t miss anything. if it shows false – lazy loading is on!


Thanks for Ayende Rahien for pointing on NHibernateUtill, this can be quite handy.