skip to the main content area of this page
Patterns and Practices

 

Subsonic 3.0 - Generate Data Access Layer and O/R Mapper using T4 Templates


Resources: List of Code Generation Tools, List of O/R Mappers

Rob Conery has released SubSonic 3.0 Preview 2 which via T4 Templates generates a data access layer for your ASP.NET Web Applications. Subsonic 3.0 is very much an alternative to LINQ To SQL if you are looking for a FREE O/R Mapper for you web applications.

Rob mentions the foundation of SubSonic being:

  • Core: Data Access and SQL Translation. This is the part that executes commands against the database, and translates queries (SubSonic's or Linq's) into SQL.
  • Surface: Working with your database. This layer "surfaces" your database and implements "IQuerySurface" (I couldn't think of a better name). In short, it exposes every table as IQueryable<T>, every Stored Procedure as an executable method, and also provides you with Aggregation queries (Count, Sum, Min, Max, etc).
  • Implementation: Your Favorite Data Pattern. This can be Repository, ActiveRecord... whatever. I've created (with this release) an IRepository pattern for you that I think covers 99% of the needs out there (you'll see in a moment) and also allows for some nice testability.

The best part, however, is that you can query your database just like you do with LINQ To SQL or the ADO.NET Entity Framework - Using LINQ!

 

            Northwind.DB db = new DB();
            var result = from p in db.Products
                         where p.CategoryID==5
                         select p;

            foreach (var item in result) {
                Console.WriteLine(item.ProductName);
            }

 

 

There is also an IRepository<T> interface for those wanting to stick with Domain-Driven Design concepts:

 

    namespace SubSonic {
        public interface IRepository<T> {
    
            IQueryable<T> GetAll();
            PagedList<T> GetPaged(int pageIndex, int pageSize);
            IQueryable<T> Find(Expression<Func<T, bool>> expression);
            void Add(T item);
            int Update(T item);
            int Delete(T item);
            int Delete(object key);
            int Delete(Expression<Func<T, bool>> expression);
    
        }
    }

 

Rob has a 4 minute video on SubSonic 3.0 as well as the download bits here.

 

Related Posts:

 


Tags: T4


Topics



 

Popular Tags



Recent Links