The Repository Pattern & Hooking Up a Database
Today I made the first steps towards re-wiring up my app to a database. I originally started out concurrently developing an in-memory datastore and a PostgreSQL database, but once I got rolling and needed to knock out a big batch of stories before my trip I focused on just using the in-memory objects and datastore.
Now that I’m going back and wiring up the database I’ve started looking into the repository pattern, which is essentially what I’m doing, and it’s a pattern that has also gained popularity around the 8th Light office. One of the best articles I’ve found about the repository pattern is actually from 2006 and the author, Greg Young, explains why he’s using the repository pattern in place of Active Record. He argues that “ActiveRecord seems to defeat the entire concept of DDD [Domain Driven Design], ActiveRecord promotes a database centric model”.
By “deferring” my database decision until now I’ve been able to focus on the functionality of my app and constructing the objects and interactions in a way that best suits my application (i.e. “domain”). Once I got over the initial learning curve, using in-memory objects and the datastores was very simple too. I could even fire up a browser and all of the in-memory objects would persist while the server was running so that I could demo the app. Last but not least, my tests were really fast, about 107 tests in a tenth of a second.
But… that all came to a screeching halt today. Implementing a DataMapper repository hasn’t gone quite as smoothly as I’d hoped, and I’m also trying to implement RSpec’s “shared_examples” for the first time so that I can just have the DataMapper repos use the same tests that I already set up for their associated in-memory objects. The shared_examples are actually really sweet, but I have yet to actually get any of those tests to pass. Fortunately tomorrow’s another day.