If you drink from the fire hose read Ayende’s blog you would notice a lot of Anti-Repository talk over the past couple years – which I fully agree with. Back in 2009 he declared repository is the new singleton, stating: My current approach for data access now is: When using a database, use NHibernate’s ISession directly Encapsulate complex queries into query objects that construct an ICriteria query that I can get and manipulate further When using something other than a database, create a DAO for that, respecting the underlying storage implementation Don’t try to protect developers Naturally he gets a slew of comments asking how he handles certain scenarios without using the repository...

LINQ and Expression Trees have enabled a ton of possibilities for us as developers, particularly in establishing some code-reducing convention based querying. I have found myself playing with Expressions in C# since their inception and will hopefully be publishing a few other posts I have queued up. For now, this post will simply demonstrate how to take advantage of a popular convention whereby each database table always has a single integer primary key named “Id” By taking advantage of this primary key convention I can save myself a ton of boilerplate code to query a table by ID. The Extension Method public static T GetById<T>(this IQueryable<T> query, int id) where T : class
{
// Create lambda parameter x of type T
var param = Expression.Parameter(typeof(T), "x");
// Given x param, extract the Id property
var...