Some time back, I watched a video David Heinemeier Hansson give a talk on ActiveResource on RailsConf. The thing that struck me is how much Rails’ ideas are connected to those of Domain-Driven Design. Watching DHH is like seeing a version of Eric Evans on speed.
The video is long, but very entertaining. And it is well worth watching even if you couldn’t care less about Rails. DHH explains in real concrete terms how to think in terms of Domain-Driven Design, even though from the sound of it, I don’t think he’s heard of the term. The subtitle of DHH’s talk is “How I stopped worrying and learned to love the CRUD”. Besides being a reference to one of the best movies ever, this title explains the views of DHH on CRUD, REST and DDD and how they fit together.
CRUD means Create, Retrieve, Update, Delete – the elementary operations on data. In many ways the POST, GET, PUT, DELETE http verbs in REST correspond to INSERT, SELECT, UPDATE, DELETE in SQL and Create, Retrieve, Update, Delete in the CRUD acronym. Thinking in REST means that you can only Create, Retrieve, Update or Delete stuff. But what about, say, making a Prospect Party into a Customer Party. This is a Business Operation on the Party object, or something, isn’t it? The Enterprisey thing to do is to create a method for it, right?
Not so fast, says DHH (and Evans!): We have a party entity. What if we instead insert a CustomerRelationship on the Party object? Or, in terms of REST:
For every enterprisey business operation, there is a new entity trying to break out.
For an excellent example of the applicability of these ideas, see The Beast Forum implemented in Rails. In not much more than 500 lines of code (yes, that is not a typo – five hundred! But there are about 1500 lines of template html code as well), they implement implement a fully featured forum, including RSS support up the wazzo (much better than PHPBB or JForum), and REST web services up the Wazzo. By following the CRUD conventions, you get it all for free!
CRUD is sooo goood! It taste like ice cream. With peanut butter! Yum!