The Impedance Mismatch
The formal mismatch between objects and relations (identity, inheritance, associations, granularity, navigation), the history from TopLink and Hibernate to ActiveRecord and the Rails effect, Fowler's patterns vocabulary, Data Mapper against Active Record, and the ORM debate argued properly in both directions.
Objects and tables disagree
Ted Neward called ORMs "the Vietnam of computer science": a project that starts easy and gets harder the deeper you go. The fair version: ORMs remove large amounts of repetitive code and make the common 90% pleasant, and they hide the costs of the remaining 10%. Engineers who know the machinery get both benefits.
Data Mapper versus Active Record
| Active Record | Data Mapper | |
|---|---|---|
| idea | an object wraps a row and knows how to save itself (account.save) | plain domain objects; a separate mapper or session persists them |
| examples | Rails ActiveRecord, Django ORM, Sequelize, Laravel Eloquent | Hibernate/JPA, SQLAlchemy ORM, MikroORM, Doctrine |
| strengths | simple, fast to start, conventions | domain model independent of persistence; unit of work batches changes |
| costs | domain logic coupled to tables; testing needs the database | more concepts (sessions, flush, detached objects) |
Martin Fowler's Patterns of Enterprise Application Architecture (2002) named the vocabulary used for the rest of this course: Identity Map, Unit of Work, Lazy Load, Data Mapper, Active Record, Repository, Query Object.