I've been doing a lot of work with Castle's ActiveRecord. In order to get my data back from the database I have been doing queries like:
var activities = Repository.FindAll<Activity>(Expression.Eq("Status", StatusEnum.Published)
This works great, except for having to use a string for the property I want to set my expression for.
How about this:
var mostPopularActivities = Repository.SlicedFindAll<Activity>(new[] { "Categories" }, 0, 5, new[] { new Order("ViewCount", false) }, Expression.Eq("Categories.Id", category.Id), Expression.Eq("Status", StatusEnum.Published));
Anybody understand this? Now it's getting really complex. What we are saying is "Get me all the activities and join categories, and get me rows 0 - 5 (top 5), and order by the activity view count where the activity's category is the one I'm looking at and the activity's status is published" Nice!? Not quite....
Now what I've done is implement NHibernate.Linq for ActiveRecord using Ken Egozi's ActiveRecordContext. I have created a nice little factory method:
1 public static List<T> FindAll<T>(Expression<Func<T,bool>> where) where T : class
2 {
3 List<T> list;
4 using (new SessionScope())
5 {
6 var db = new ActiveRecordContext();
7 list = db.Session.Linq<T>().Where(where).ToList();
8 }
9
10 return list;
11 }
Now I can do nice code using
Linq/Lamda:
var activities = DomainFactory.FindAll<Activity>(a => a.Status == StatusEnum.Published);
There we have it....string free code!