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

author: Craig | posted @ Sunday, June 29, 2008 2:17 AM | Feedback (0)

Welcome


My name is Craig Neuwirt and I am principal consultant and partner of Improving Enterprises. I am the proud father of 2 girls and a boy ages 5, 3 and 8 months. My limited or nonexistent blogging is directly related to that. However, my friend Ayende award me, and I quote, "the first hostile blogging award". Ayende was also nice enough to provide me a blog so the least I can do is try and use it. Despite my limited free time, I am an active contributor to Castle and RhinoTools open source projects. I have had several comments that mailing groups are not the best medium to share my information so I will try and utilize this blog to do that. They will not be plentiful nor beautiful, but if I have something to say, i'll see it here.

author: Craig | posted @ Friday, March 28, 2008 6:15 AM | Feedback (10)