What makes a framework an ORM?
Posted by Joe Rinehart at 3:50 PM
13 comments - Categories:
Frameworks
Earlier today, I posted an entry asking if ColdFusion has no real ORM frameworks. It wasn't about any frameworks in particular: it was about terminology and concept. In the comments, there's a lot of questions about what makes a "true" ORM and whether it's a definition worth following or a term that should be loosely applied to anything that helps out with SQL.
In the overall world of OO, there really is a series of criteria. ORM technology exists to solve the differences between object models and relational schemas. Here's the generally accepted list of the problem areas to be solved (from what I can gather, feel free to point out more).
Granularity
A User may be composed of two Address objects (homeAddress and businessAddress), but the database may choose to store this in one table to avoid joins. An ORM should be able to resolve the granularity mismatch between the two.
Inheritance
A User might be extended into MaleUser and FemaleUser each with different composed objects (avoiding obvious jokes here, but it was an easy example!). An ORM should be able to resolve this difference, allowing a user to save the state of both User subclasses.
Polymorphism
As soon as we introduce inheritance, we obviously have to deal with polymorphism. If a UserGroup has-many User, and we ask the ORM to find all Users that are part of the Group, it should be able to understand, discrimate between, and populate the resulting collection with User, MaleUser, and FemaleUser instances as approriate, keeping the is-a contract in place.
Identify
This one's a killer. In an object model, it's perfectly possible to have two instances of a single object (User with Id of 314). In the database, primary keys keep the equivalent from occuring.
This introduces a problem of identity mismatch. In the database, there's one definition of identity (one record, Id of 314. In the object model, there are two: instance identity (two instances) and entity identity (two instances, but _one_ entity, id = 314).
An ORM should be able to resolve the concept of identity within its units of work against the relational database.
Associations - Plumbing
Object models relate instances via references. Databases do it via foreign keys. First, an ORM must be able to solve this plumbing mismatch, usually accomplished through foreign-key-identified proxy collections / objects or lazy loading.
Associations - Navigation and Direction
A second problem with associations is directional. An object reference is, by nature, one-way. A foreign key association, however, is multidirectional. An ORM should constrain the free navigability of the data model into the directions specified by the object model.
Associations - Multiplicity
It gets worse and worse for associations. In a relational database, any foreign key reference is either one-to-many or one-to-one. When looking at an object model, however, it is impossible to determine the multiplicity of a collection-based association property: it could be one-to-many or many-to-many. An ORM needs to be able to solve this mismatch, providing a solution that allows for both one-to-many and many-to-many relationships between objects to be persisted.
ike wrote on 11/03/08 4:48 PM
I don't think I'm following what you're saying the item you describe as "identify". I understand the difference between the db's concept of identity and the OO concept of identity. But then I don't understand the problem being solved in the last sentence when you say "An ORM should be able to resolve the concept of identity within its units of work against the relational database."I would gather that generally speaking the external ORM would introspect the provided object, find its class name and then look for a definition in its config of how that particular class is to be persisted?