/ Database

Seven reasons you should try SubSonic

SubSonic is a query tool for .Net data access. If you haven’t tried it out then you really should and here’s why:

It’s simpler than, for example, nHibernate

Don’t get me wrong I like nHibernate a lot it’s amazingly powerful, flexible and mature. I’ve worked with it on many projects and admire it a great deal. But if you want to go from nothing to a working project with it you’re going to have to do some serious work and spend some time understanding the nHibernate way.

You can get started with SubSonic and have data access up and running in half an hour and that includes watching the getting started video. In fact I can summarise that video in 7 steps:

  • Download SubSonic
  • Add a reference to SubSonic.Core to your project
  • Add a connectionString to your project’s config file
  • Modify the Settings.ttinclude file to specify your Namespace, ConnectionStringName and DatabaseName
  • Add the tt and ttinclude files to your project, if they don’t run automatically click ‘Transform All Templates’ button at the top of Solution Explorer
  • Start accessing your data from the generated classes
  • Grab a kabob (I have no idea what a kabob is), this step is optional but apparently Rob thinks they’re good.

LINQ

Want to query your data using Linq and lambda expressions, no problem you get that out of the box.

You can target databases that aren’t SQL Server

OK so you’re thinking what’s the point, I’ve got Linq2Sql if I want a simpledata access method. Well SubSonic works with MySQL, SQLite and there’s Oracle support almost ready to go.

t4

SubSonic is simple to get started but that doesn’t mean it’s inflexible, the classes that SubSonic creates for you are generated using t4. If you don’t like the way your classes are being generated by the standard templates you can dive in and start modifying them yourself. Grab a copy of Clarius’ T4 Generator Toolkit to get syntax highlighting and other editor goodness within Visual Studio.

Built in testing

So you’re writing unit tests that need some data, but you don’t want to have to build a database dependency into your unit tests. SubSonic’s got that covered for you if you’re using the ActiveRecord templates you can simply specify a test database in your connection string.

<add name="MyDatabase"connectionString="Test" providerName="System.Data.SqlClient" />

You’ve now got an in memory database that you can populate with whatever you need in your test setup so you can do something like this:

[Setup] 
public void SetupTest() 
{
  Person.Setup(new Person { Name = "John Smith", Age="34" }); 
} 
[Test] 
public void TestIsOverThirty() 
{ 
  // Arrange 
  Person person = Person.FirstOrDefault(p => p.Name == "John Smith"); 
  // Act 
  bool isOverThirty = PersonService.CheckIsOverThirty(person); 
  // Assert 
  Assert.Is.True(isOverThirty); 
}

No need to create a database, runs in memory and you can unit test your code painlessly, what’s not to like?

SimpleRepository

Don’t want to build your database first and then generate classes well you can go the other direction. SubSonic’s SimpleRepository allows you to build your classes and get them to automatically generate your database schema for you. If you just want to get on and build a really simple app this is a great way to go.

It’s not dying and there’s a lot of help out there

There’s a whole load of documentation on the SubSonic site covering everything from getting started to contributing to the project and including FAQs, quickstart videos, how to customise your templates and much much more. There’s also a community of folks answering questions on stackoverflow.com so if you get stuck you’ve got a quick way to get help and answers. Oh and SubSonic’s not dying.

In conclusion I honestly think SubSonic’s an amazing tool to have available, I know there’s plenty of others out there but why not give it a try. If you’ve got questions about it feel free to comment or send them straight to stackoverflow.