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.
- Add a reference to SubSonic
- Add a connection string
- Set the connections in the templates
- 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.
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).