Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help me to connect inheritance and relational concepts

I was talking with a programmer pal of mine about inheritance and its use in designing models. He's a big proponent and I'm a little more tepid. Mostly because I tend to design systems from the bottom up: Database -> Application -> Presentation (honestly, I'm not much of a front-end guy, so I often leave presentation entirely to someone else). I find that relational database systems don't support inheritance without a lot of 1-to-1 relationships to other tables.

If I'm designing from a conceptual standpoint, an Administrator is a User is a Person. Starting from the database up, an Administrator is a User with UserType = "Administrator". Reconciling these approaches seems difficult to me, and therefore I only use inheritance with non-persisted objects.

What's wrong with my thinking? Why is inheritance such an oft-celebrated aspect of OO if it has these inherent incompatibilities with traditional relational structures? Or, is it me who's not mapping inheritance properly to relational data? Is there some guidance out there that might clear this up for me?

Sorry for the rambling question and thanks in advance for your answers. If you include code in your response, C# is my "native language".

like image 742
Chris McCall Avatar asked Aug 26 '09 21:08

Chris McCall


3 Answers

This has been popularly termed the Object-Relational Impedance Mismatch:

Schema inheritance — Most relational databases do not support schema inheritance. Although such a feature could be added in theory to reduce the conflict with OOP, relational proponents are less likely to believe in the utility of hierarchical taxonomies and sub-typing because they tend to view set-based taxonomies or classification systems as more powerful and flexible than trees. OO advocates point out that inheritance/subtyping models need not be limited to trees (though this is a limitation in many popular OO languages such as Java), but non-tree OO solutions are seen as more difficult to formulate than set-based variation-on-a-theme management techniques preferred by relational. At the least, they differ from techniques commonly used in relational algebra.

See also: Agile Data, C2 Wiki

like image 119
John Kugelman Avatar answered Oct 26 '22 04:10

John Kugelman


You should read the Patterns of Enterprise Application Architecture. It will show you how inheritance patterns can and should be expressed relational structures.

The important thing to remember is the inheritance is conceptual modelling thing. Relational structures and OO code are just materialisations of those conceptual models, and shouldn't be considered the be all and end all of object orientation.

There is an tutorial on this type of thing here.

like image 43
Preet Sangha Avatar answered Oct 26 '22 06:10

Preet Sangha


The gulf between the relational paradigm and the OO paradigm is often discussed.

Basically, to provide a mapping between RDBMS and OO, you must sacrifice some capabilities of both systems. You have to decide which side your compromise is more heavily weighted toward.

Both paradigms are very powerful and useful. But trying to map between them seamlessly is an unresolved problem.

like image 44
Bill Karwin Avatar answered Oct 26 '22 04:10

Bill Karwin