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.

 

ActiveRecord & Atlas POC

Myself and Pasha are now starting to build a little thing we came up with and we thought that it will be a good idea to use this little baby to learn about the new technologies out there. But you can’t learn everything at once right? so we decided to start with Atlas. In addition, we want to take advantage of some sort of infrastructure to perform the common tasks we are all familiar with while developing an application like (1) Data persistence (2) Lazy Loading (3) Caching (4) Data Validation (5) Nullable fields (6) Transaction support and their partners (7) Authorization&Authentication (8) Logging and such. Looking around the net, it seems that ActiveRecord (I see it as an abstract shape of NHibernate, one that makes you smile while mapping your entities and querying them instead of pulling your hair in anger) which is part of Castle Project, support tasks 1-6 pretty damn good so it will be a huge part of my POC. I’m planning to share my insights with you, including the “How To”, “Why To” and the complete source code of the POC which will include the skeleton of our infrastructure.


You can download the Castle version(Includes ActiveRecord) I use from here (It is from August 27th, 2006).