CRUD, REST, DDD, Rails - these are a few of my favorite things
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:
PUT /party/135122/relationships
<customerRelationship>
<keyAccountManager>http://my.company.com/employee/652312</keyAccountManager>
<validUntil>2007/08/01</validUntil>
<customerStatus>GOLD</customerStatus>
</customerRelationship>
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!
Comments:
[DHH] - Feb 27, 2007
Thanks for the kind words. DDD is indeed one of my favorite programming books. Lots of inspiration from Mr. Evans.
Johannes Brodwall - Jan 9, 2008
Hi, Aksel
Yes, no, I don’t know. If you have a large domain model you might of course want to manage it. Any large domain that’s not managed can be bad. SQL helps you some with this, but WSDL really doesn’t.
[aksel] - Jan 9, 2008
It’s sort of an interesting idea, but don’t you end up with an unmanageable explosion of models?