I have run into this problem a couple of times in my career, and have never been very happy with the solution, and am presented with it again on a project I am doing in ASP.Net MVC, C#, SQL Server 2008:
Imagine I have a Person type (class). I further have types Mother and Father that extend Person. Father and Mother are very similar: they both have a property called "Children", which is a collection of type Person. A "person" can either be represented with the base Person class, the Mother class, or the Father class. From this example you can see that I have both inheritance (is-a) and association (has-a) relationships going on.
I want to build and store family trees in a SQL Server using these OO types. I think I want to have these 3 tables: Person, Mother and Father. Every object will have an entry in Person, and possible and entry in Mother or Father if appropriate (with a FK relationship to Person). Additionally, I will need some crosswalk tables to store the relationships between a Mother record and any child records, and the same with Father.
Does this sound like a good strategy for storage?
How would you efficiently query this for deep and wide family trees?
The problem I get hung up on is the polymorphic nature of the data being returned at and given node in the tree. If this were just a tree of Person objects I would use a Recursive Common Table Expression. But 3 different shapes of data might be returned for any give node, which I would like to map to one of the 3 OO types in C#. I can obviously do the recursion in C# or a stored procedure, but I am not very happy with the performance of such solutions in the past. Additionally, how do I insert records naturally? I have always have to insert in the right order (Person, then Father or Mother) in the past because of the FK relationship enforcement in SQL Server.
Is there a framework out there that will handle this type of ORM for me?
Edit:
To make it clear, the solution I require is to retrieve the entire family tree for display, and be able to add and edit nodes to the family tree. I think the best solution would be to do this in one query in a deep tree. How to design the schema, store, and retrieve is what I am asking about?