I’ve just watched the DataMapper presentation from Mountain West 2008, and it’s very interesting. I’ve been thinking we need a high-level super-abstraction library for Ruby for some time, and DataMapper (DM) looks like it might grow into fitting that bill.
What’s wrong with what we’re using now? Let me count the ways – or rather, let me count how many different sources to and from which I am reading and writing data in a Rails app of mine today:
- An RDMBS (Postgres), using ActiveRecord
- A number of resources, using ActiveResource
- A document-based database, using a custom i/o library
- YAML config files, using a constant read from a file on server load
- Cookies, via ActionController
- (not really a full point) data structure information in Model files
That’s too many. And I see no sign of this trend slowing down – why, I just found out about the new project today, yet another library to address document-based databases. We have at least three gems to read and write to CouchDB. Thingfish uses Sequel. If you use Amazon S3 that’s yet another. Enough!
It’s more and more obvious that these new developments are being written at the wrong level of abstraction. I don’t know what we can do about Cookies but all the others – RDBMS, RESTful resources, YAML files, etc – are the same types of data and should be accessible by a common API within Rails or Merb or plain Ruby or anywhere. So why can’t we access them all via a common method?
The correct way to do this is to have a single ORM at the highest possible level of abstraction. Storage adapters can be added to it as drivers. Then, if you need to split storage types for whatever reason, you can configure that on a case by case basis.
DataMapper enables this, and provides a plug-in system – if you can implement 10 or so basic actions in your storage system, and support 5 or so basic data types, you should be able to use it transparently. To me, this is a very appealing step forward. There are many types of data storage, all with their strengths and weaknesses. If we can flexibly and transparently include pretty much any useful type of storage into our programs using a single consistent API and with just a little config, once, in one place, that’s a huge win.
Why not just improve ActiveRecord? I think it’s too late for that. AR is a huge, tangled mess which many believe should just be scrapped and re-written from scratch, me included. Well, the good news is that DM has basically done that, and it’s smaller, faster, more modular, cleaner and – best of all – mostly compatible.
UPDATE: Whilst looking at the weblog of Rails Core member Rick Olson, aka Technoweenie, for info on ActiveDocument I came across this wonderfully candid comment on AR associations:
I can point to a few other more complex areas of the associations code (the crazy eager joins code is why I personally haven’t used :include in any of my apps in over a year).
Straight from the horse’s mouth! Couldn’t agree more.