SubSonic 3.0, A Closer Look at ActiveRecord

SubSonic 3.0, A Closer Look at ActiveRecord

Sep, 26, 2011

SubSonic 3.0 ActiveRecord is the easiest way to get up and running with SubSonic. It's a database first approach using T4 templates for class generation. This approach will give you a very thin DAL to your database.

The four easy steps to get started are documented on

  1. Add a reference to SubSonic
  2. Add a connection string
  3. Set the connections in the templates
  4. Add the T4 templates to your project

VS will chug on the templates for a few seconds and then generate a file for each T4 template.


The Context.cs file contains 1 class representing your database, implementing SubSonic's IQuerySurface interface.

SubSonic's IQuerySurface Interface

This class contains all the actual data transaction code, offering up methods to execute and perform aggregate functions on your database.


Structs.cs defines the tables structures. A class is generated for every table that derives from SubSonic's DatabaseTable class.

SubSonic's DatabaseTable Class

DatabaseTable holds a List of all the columns that comprise the table in the database. The DatabaseColumn class represents each column, containing properties mirroring the datbase configuration.

SubSonic's DatabaseColumn Class


The ActiveRecord.cs file contains LINQ queryable classes for each table in your database. Each class represents 1 table in your database and implements SubSonic's IActiveRecord interface.

SubSonic's IActiveRecord Interface

The first line of the generated class defines a variable for SubSonic's generic IRepository.

SubSonic's IRepository Interface

The ActiveRecord.cs file is probably contains your most interacted with methods for querying your database:

  • T SingleOrDefault
  • bool Exists
  • IList<T> Find
  • IQueryable<T> All
  • PagedList<T> GetPaged


The StoredProcedures.cs file contains a method generated for each stored procedure in the targeted database.

SubSonic's StoredProcedure.cs

The method sets the target stored procedure, adds the parameters and returns the object ready to be executed in the preferred way ("blindly", as a DataSet, as a Reader, as a Scalar, or as a TypedList of the table's ADT).

Tags: SubSonic, ActiveRecord


Recent Posts

Entity Framework update-database "Could not load file or assembly or one of its dependencies. The system cannot find the file specified."

I spun wheel for a while on this one. Every time I ran the update-database or add-migration command I was getting "Could not load file or assembly 'MyProject.Name' or one of its dependencies. The... Read More

You get nahzing, Lebowski!

Windows 8 CP to Windows 8 RP doesn't give you many choices on what I get to keep when I upgrade. Guess I won't be needing that "Help me decide" link.... Read More