jgaskins edited this page Jan 17, 2013 · 5 revisions
Clone this wiki locally


  1. Installation
  2. Configuration
  3. Mappers
  4. [Queries/CRUD](wiki/CRUD Operations)

Persisting Objects

In Principles of Enterprise Application Architecture by Martin Fowler, there are two common methods of object persistence: Active Record and Data Mapper. The Active Record pattern is the one most Rubyists are familiar with due to the gem of the same name.

I will not be contrasting all of the details of each pattern, but they differ primarily in one way. The AR pattern gives domain objects the ability to persist themselves whereas the DM pattern relies on third-party objects to persist them.

Perpetuity follows the Data Mapper pattern. This gives us the ability to serialize any object we choose into any data source we support. The only requirement is that we declare mappers for these objects.

What benefits does the Data Mapper pattern give?

The primary benefits are decreased coupling with the database and fast unit tests.

The ActiveRecord gem and some other Active Record-pattern implementations require not only a connection to a database in order to instantiate the objects, but in order to associate one object with another, the associated object needs to be persisted. This means unit tests are not possible unless you stub or mock all database interaction.