Persistence Ignorance and ActiveRecord

Jimmy Bogard recently tweeted that Persistence Ignorance does not apply to the ActiveRecord pattern. I couldn't agree more because ActiveRecord is about creating a strong coupling to the database schema.

Persistence Ignorance is about keeping your business objects ignorant of the underlying mechanism used to persist them and the schema in which they are persisted. In the case of ActiveRecord, it doesn't apply because these objects are intimately coupled to the schema itself. ActiveRecord objects may implement a special base class, have lots of decorator attributes and other such things. But ActiveRecord objects should also be simple DTOs--ones that can be easily serialized across the wire if necessary.

Just because we are tightly coupled to a database schema does not mean we have to be coupled to any particular database product or vendor. In other words, we should still be able to swap out a relational database using another one from another vendor without having to modify our application code.

Using CQRS this becomes less than trivial because the only place where we use a relational database is for reporting. Incidentally, this happens to be the ideal location for a relational database because they're really, really good at querying data.

By using the proper abstraction to isolate your ActiveRecord-based technology, you should be able to swap out not only your database product, but also your data access technology and your application shouldn't even notice.