Insights and outlooks on software development

S'true

On migrations API:s in .NET

Friday, November 28, 2008 by Thomas L

So I absolutely love Rails' Migrations. The functionality is kick-ass, and the API is super-nice. See for yourselves:

create_table :users do |table|
  table.column :name, :string
  table.column :login,  :string, :null => false
  table.column :password, :string, :limit => 32, :null => false
  table.column :email, :string       
end

I'd like to have this in the .NET land as well, and with an as nice API as well. There is the Machine.Migrations by Jacob Lewallen with an API that looks like this:

Schema.AddTable("users", new Column[] {  
  new Column("Id", typeof(Int32), 4, true, false),  
  new Column("Name", typeof (string), 64, false, false),  
  new Column("Email", typeof (string), 64, false, false),  
  new Column("Login", typeof (string), 64, false, false),  
  new Column("Password", typeof (string), 64, false, false),  
}); 

(The code is blatantly stolen from a blog post of Jacob's.)

There is also the migratordotnet project which has a similar API, I actually like this API a tiny bit more than the Machine.Migrations one, but it's more or less the same. But the main thing is that the API for defining the migrations is somewhat hard to read, and readability is an extremely important property of code.

What I'd like to have is a API looking somewhat like this:

Database.AddTable("Users").WithColumns(user => Is.String.WithSize(64), userId => Is.Int.WithSize(32));

This would actually be implementable in c#. I haven't done anything to make this API work with Machine.Migrations or migratordotnet as I'm only playing with a thought as of now, but it's quite fun to see how much you can do with closures.

0 kommentarer: