📎 Webclip
ORM Is an Offensive Anti-Pattern
The article argues that ORM violates object-oriented principles by tearing objects apart into passive data holders and database-handling machinery. It says ORM exposes the relational model across the application, does not really hide SQL, and makes testing awkward because objects depend on session factories and database wiring.
It proposes SQL-speaking objects as the alternative: separate interfaces for plural and singular concepts, immutable objects, and database interaction encapsulated inside those objects. It also shows how decorators can cache data for performance and how transactions can be wrapped in a callable object.
Reading notes#
- ORM is presented as an anti-pattern that splits one cohesive object into data on one side and persistence logic on the other.
- The article says ORM does not hide SQL; it spreads SQL or SQL-like queries through the application.
- Testing becomes difficult because objects depend on SessionFactory and database-backed integration tests.
- The suggested alternative is SQL-speaking objects that encapsulate persistence inside the object boundary.
- The design uses two interfaces, Posts and Post, to represent the table and a row.
- Posts lists rows and adds a new row, while Post exposes id, date, and title through methods.
- The implementation examples use JDBC-style code inside PgPosts and PgPost, so the database access stays inside the objects.
- Performance concerns are handled by a decorator that caches date and title values.
- Transactions are wrapped in a Txn class that accepts a callable and commits or rolls back around it.
