I’ve been using SubSonic for about a year now, and I’m a big fan. As the website says, SubSonic is a open source toolset for .NET developers that helps a website build itself. Essentially what SubSonic does is automatically generate your data access layer for you. It saves you from having to write the same boilerplate code over and over. Developers like to talk a lot about improving productivity, and SubSonic actually helps in that regard. The latest version, 2.1, was released on Tuesday.
SubSonic is known as an Object-Relational Mapping (OR/M) tool. What that means is that it generates objects that reflect your database structure. So if you have a table called “User” it will create an object called “User”. Unlike most OR/M tools however, SubSonic prefers convention over configuration. This is my favorite part about SubSonic – there are no mapping files! You don’t need to say that “User” is a table you’d like to use, and that the “Username” column is a string. SubSonic figures that out on its own. Everything just happens automagically.
I like to run SubSonic manually on the command line, so that I can stick the code it generates into a class library. You do this by running SubCommander. The objects SubSonic generates by default are Active Record objects. You use them like this:
User user = new User(); user.Username = "mastermaq"; user.Save();
The newest version of SubSonic also supports what’s known as the Repository Pattern, for developers who don’t like the Active Record way of working with objects. You can learn more about that here.
Also new in SubSonic 2.1 are query factories, which let you write fluent code like this:
return new Select() .From<User>() .Where(User.Columns.Username) .IsEqualTo(username) .ExecuteSingle<User>();
Doesn’t that look wonderful? It almost makes working with the database enjoyable!
If you are still writing your data access code by hand, I’d definitely suggest taking a look at SubSonic. If you’re using a different OR/M tool, you might want to see how SubSonic compares. It currently works with SQL Server 2000/2005/2008, MySQL, and Oracle.