views:

42

answers:

1

I'm moving a project off of a custom DAL and onto Nhibernate but I've run into a tricky mapping scenario that I don't know how to resolve. Essentially, there are sub classes of a subclass without a discriminator value.

The primary subclass has a discriminator value so that was trivial. The issue arises when I get to the third level. The only difference between the sub-sub types is which table references them (in a HasOne style map).

Any help would be greatly appreciated, thanks :)

+1  A: 
If you want the third-level objects to be records in the same table, I'd add a discriminator to the third-level type. It can be a calculated property based on the parent type. Using FluentNH, I think it does have to be visible to the mapper (you can't use the tricks that vanilla NHibernate has to reflectively examine private members). That's if you want to have them all in the same table. If you don't care too much about table count in your schema, you simply cannot include a discriminator, and/or the schema definitions of the third-level types are different enough that mapping to a single table would provide "odd" results (like a lot of null columns or very general/nondescriptive column names), then simply create a mapping for each concrete subtype to a different table. You can also de-normalize the schema. Since it's a HasOne() relationship, the subclass can contain the columns of the third-level class using a Component mapping. If all of the third-level types map to similar fields, and you think it'll be unlikely that this relationship will change from 1:1 to 1:n, this is probably the way to go.
KeithS
Yea, I just figured that out (thank god for unit tests that spit out the nhib sql) - thanks though!
Chance