OK, so why hasn't anyone told me of the rockage that is ActiveRecord? I was halfway through writing my first NHibernate XML file when Oren himself pointed me to StoryVerse, whose code is easy enough to follow that I was able to pick up a practical use of ActiveRecord without too much hassle.

Gives some context to the attribute vs. XML file debate which was passing me by. And I have to say, the thought of bypassing XML file management is pretty appealing for a hillbilly with the first signs of carpal tunnel. Data schema dependency be damned, that's some bit of magic when you can dump a bunch of attributes on a class and not write a single line of data access code.

Which brings me to the real reason for this here hootenanny. One of my classes is called FiscalSystem. Its implementation is not worth explaining except for one of its properties. To wit: StateProvince a class which in turn has a property called Country. I.E. A FiscalSystem belongs to a State/Province which belongs to a Country. (I didn't do the reverse relationship because the app is small enough that I don't need it.)

So to get a FiscalSystem's country: myFiscalSystem.State.Country.Name.

Next comes the query: Show me all FiscalSystem objects that apply to a particular country. Here's the query I tried:

    ICriterion[] criteria = new ICriterion[]
            Expression.Eq( "State.Country.Id", countryID )
    FiscalSystem[] systems = ActiveRecordBase<FiscalSystem>.
FindAll( criteria );

Side note: In reality, all I actually wanted on my FiscalSystem class were StateName and CountryName properties because I didn't need the semantics of a full State or Country object anywhere else. But I was unable to determine a way of having ActiveRecord look up information from more than one table (i.e. State and Country) for my FiscalSystem class. So I had to create objects for each of them.

No dice. The error is long gone now but it went something like "Could not resolve property name 'State.Country.Id'". Works if I query at the State level, but not at the Country level. Which leaves me to believe it has something to do with reflection.

Incidentally, you get a similar error if you try to bind a GridView to a list of FiscalSystem objects and use a BoundField that references State.Country.Id (or anything at the Country level). You get around that be using a TemplateField instead with Eval( "State.Country.Id" ).

My alternate solution was to dig into HQL which is amazing in its own right:

    SimpleQuery<FiscalSystem> query = new SimpleQuery<FiscalSystem>( @"
    FROM FiscalSystem fs
    WHERE fs.ParentState.ParentCountry.Id = ?
    ", countryID );
    FiscalSystem[] fiscalSystems = (FiscalSystem[])ExecuteQuery( query );

Man, there is a price to be paid living out of the loop down in the caribbean all these years.

Kyle the Impressionable