Model + Controller = Unified Resource?

rest mvc

Wed Apr 16 12:05:00 -0700 2008

Harry Fuecks thinks MVC might be overrated. In a world of web resources, why not combine models and controllers together? Imagine replacing ActiveRecord (or my new favorite, DataMapper) with a server-side version of ActiveResource. GET, POST, PUT, and DELETE are filled in automatically, and you can extend their functionality and add suport methods.

He also argues that what we really want is RUD, not CRUD. The distinction between create and update is subtle, and hardly important if you think of resources as documents. (Document-oriented databases are poised to replace relational databases - we may soon be thinking in terms of documents instead of records even on the backend.) Think of your filesystem, the seminal document-oriented database: when you write a file, does it matter if one exists already? Not usually. File.open(‘test.txt’, ‘w’) { |f| f.puts “hello” } behaves the same way regardless of the whether a file by that name previously existed or not.

This server-side ActiveResource might look something like:

class Post < Resource
  has_one :author
  has_many :comments, :dependent => :destroy

  def get
    super
  end

  def put
    super
  end

  def delete
    super
  end
end

It provides both the controller and the model. The superclass for each of the three public-facing methods handles writing it to the backend store; the relationships described by has_* give both the routing config and the database relationships.

This is more speculation than a real idea, but I found it to be an engaging thought experiment.