Multi-Table Entity Support for ActiveRecord

NHibernate supports the ability to map a single entity to multiple tables. Now I know this is not encouraged, but it is sometimes necessary. I just started a project in which the existing code base uses the ASP.NET Membership Provider. The existing software uses ActiveRecord and wanted to represent the concept of a user with several fields for the membership database. The user in the membership database is spread over several tables so the ActveRecord model used had lots of formulas which really smelled.

To understand what NHibernate can do, Ayende has a really nice post on this subject.

To avoid the formula clutter, I added ActiveRecord support for this. T

Here's an example of how to mark an ActiveRecord class to join multiple tables

[ActiveRecord("People")
JoinedTable("Addresses", Column = "person_id")]
public class Person : ActiveRecordBase
A new attribute, JoinedTable has been introduced that identifies the additional table to be joined an the column used to join the, Multiple of these attributes can be present. Here's how you would identify properties, field's, any's or components that come from the alternate table
[Nested("name_", Table = "Addresses")]
public FullName FullName
{
get { return _fullName; }
set { _fullName = value; }
}

[Property(Table = "Addresses")]
public string Address
{
get { return _address; }
set { _address = value; }
}

[Field(Table = "Addresses")]
public string City;
As you can see, the [Property], [Field], [Nested], and [Any] attributes now accept a Table property which links to a Table in a [JoinedTable] attribute
Cheers. Craig

Print | posted on Sunday, June 29, 2008 2:17 AM

Feedback

No comments posted yet.

Your comment:





 
Please add 6 and 7 and type the answer here:

Copyright © Craig

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski