Posts
18
Comments
10
Trackbacks
0
Castle ActiveRecord Async data fetching

As I may have said before I am working on a project that makes use of Castle ActiveRecord as the ORM to map the data in the database to the objects in the application. The project, as all applications should be :P, is a windows application and this therefore lends itself to some nifty async data calls to fetch our data while not locking the UI.

This is however were we run into a problem: It seems that a vanilla implementation of ActiveRecord is not happy when you fetch data on one thread and other related data on another thread.

   35 [ActiveRecord("Company", Schema = "dbo")]

   36     public class Company : BaseObject

   37     {

   38         [HasMany(typeof(Person), Table = "Person", ColumnKey = "CompanyId", Lazy = true, Where = "IsDeleted=0")]

   39         public virtual IList<Person> Employees { get; set; }

   40     }


This is easily show (as in the example) by creating an object that has a list of child objects that has been marked as list. In this case we have Companies and People. Companies have many people as employees. We don’t want to fetch all employees every time that we fetch a company so we mark it as lazy.

We will fetch the company in an async load and in the call back try and fetch the linked people. This is not really a perfect example since obviously one would rather fetch the small company object synchronously and then retrieve the large list of people asynchronously, but it works for this example.

When trying to fetch the list of employees in the originating thread a "failed to lazily initialize a collection, no session or session was closed" is thrown. This is the problem.

We are working on a solution to this rather annoying problem and will keep you posted when/if we find one.

Looks like we have a SOLUTION! Well most of one, check out www.darkside.co.za for help.

posted on Thursday, September 11, 2008 6:50 AM Print
News