SubSonic 2.1

subsonic 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.

You can download SubSonic 2.1 from CodePlex. Check out the SubSonic website for more information, and also Rob and Eric‘s blogs.

2 thoughts on “SubSonic 2.1

  1. Does SubSonic introspectively build your data access layer by looking at your tables? Or does it generate code for your database later?

    If I ever need to do a .NET project, I’ll probably use SubSonic.

  2. I think the answer to your question is both. It looks at your tables and their columns to generate objects with the appropriate properties. Those objects are Active Record objects, which means they know how to load, update, and save themselves. SubSonic also generates code to call your stored procedures, and provides more generic data access, such as with the query factories that I showed above.

    Not exactly sure I understood the question, so hopefully that helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s