views:

71

answers:

1

I am using ActiveRecord in order to store my classes in the database.

I have classes hierarchy, say it Man and Woman inherited from class Human. Human database table contains field Sex which is 0 for man and 1 for woman. I'd like to be able to load class Human with regards to Sex field, that is, if Sex is 0, loading of class Human should actually create instance of class Man (thus reading some additional fields from Man table besides of fields from Human table).

Is it possible with the help of CastleProject ActiveRecord attributes? If not, is it possible to do with overriding some overridable methods?

+1  A: 

You are talking about Single Table Inheritance

The Sex column in your table will serve as the Discriminator used by NHibernate.

EDIT: If you have additional fields for Man, you can either put them in the one large table or split them out and use Class Table Inheritance (which means a JOIN whenever you fetch Men objects) since it gets the Human fields from the Human table and the Man fields from the Man table.

Nebakanezer
Thanks, Nebakanezer, your reply is very useful. However, there is a problem that I always get Human class instead of Man or Woman. I did not assign DiscriminatorValue for this class, but did that for Man and Woman, however, this does not help. I am using FindAllByProperty of Human class and expect it will return me a list of Man and Human instances according to Sex field.What did I do wrong?
Alex
Found the solution myself - my subclasses (Man/Woman) were internal while Human was public. THat was the reason of constant generation of Human instance.
Alex