Paging an ActiveRecord Collection Association

(update: Added more information to the example.)After discovering how to count an associated collection without initializing it, I wondered if it was possible to page an associated collection in Castle’s ActiveRecord.After a little searching, I found out it is possible to page a collection (and not that much different than counting an associated collection). In ActiveRecord, there are two ways to page a collection.Given the ActiveRecordBase class SalesTeam

[ActiveRecord]public class SalesTeam : ActiveRecordBase<SalesTeam>, IAddressable{    [HasAndBelongsToMany(typeof (SaleListing),        Table = "SalesTeamSaleListingView",        ColumnKey = "SalesTeamId",        ColumnRef = "ListingId",        Lazy = true,        Access = PropertyAccess.FieldCamelcase)]    public IList<SaleListing> PropertiesForSale    {        get { return propertiesForSale; }    }}

Inside an instance of a SalesTeam Object

Execute(delegate(ISession session, object obj)    {        return session.CreateFilter(PropertiesForSale, "")          .SetFirstResult(startIndex)          .SetMaxResults(maxResults)          .List();    });

Outside an instance of a SalesTeam Object

SalesTeam team = SalesTeam.Find(1);IList list = (IList)ActiveRecordMediator.Execute(    typeof(SalesTeam),    delegate(ISession session, object obj)    {        return session.CreateFilter(team.PropertiesForSale, "")          .SetFirstResult(startIndex)          .SetMaxResults(maxResults)          .List();    },    team);

3 Responses

  1. Cheers for this – will have to have a play with it..

  2. Thank you for this simple explanation.

    I am trying to apply this in an ASP.NET webapp, but I get an exception: “the collection was unreferenced”.

    I read somewhere else that it is because I should be using the same session when loading the instance (team, in your example) as when executing the query.

    Then, I found that I should be using a SessionScope to make sure the session is kept “alive”. However I still miss one piece: how do I get the session object?

    Any advice about this?

  3. [Sorry for re-posting.]

    I found the solution to my problem: I was creating the SessionScope in global.asax in the Application_Start method, but this would not work. I created a class Global.cs associated to global.asax and registered an event handler on BeginRequest and EndRequest in the constructor of the class (not Application_Start). And if I create the SessionScope in the OnBeginRequest handler, it works fine now.

    I found example code at:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: