I've just discovered extension methods and I love them so much that I'm scared about their good use...
I have a 3-tier Asp Net app. My DAL is based on Entity Framework, my UI is aspx pages and my business logic used to be the common classes with LINQ to entities queries like this:
public List<Site> GetAll()
{
return db.Sites.Include(s=> s.City).ToList();
}
that you need to instantiate and use like this:
CSite objsite = new CSite();
List<Site> sites = objsite.GetAll();
Then I discovered Iqueryable, so this way I can "re-use" my queries like this:
public ObjectQuery<Site> GetAll()
{
return db.Sites.Include(s=> s.City);
}
public IQueryable<Site> Filter(IQueryable<Site> query, string filterWord)
{
return (from s in query
s.Name.Contains(word)
select s);
}
List<Site> sites = objsite.Filter(objsite.GetAll(),"filter word").ToList();
The Filter() method just applies a where clause on GetAll() iqueryable and this is awesome until I discovered extension methods, so I can handle it like this:
public static IQueryable<Site> Filter(this IQueryable<Site> query, string word)
{
return (from s in query
s.Name.Contains(word)
select s);
}
And this is even better because now I have intellisense for my queries like this:
List<Site> sites = objsite.GetAll().Filter("filter word").ToList();
Now, this is the part where I'm scared and is because of three things:
Do you think this is a good approach to an n-tier app?, is this a good design pattern or just a lazy solution?
Given the requirement of extension methods to be static methods under static classes, my business tier would be all static, is this a good approach?, should I put the non-extension methods under the same static class? (ex. GetAll() or AddNew())
Being Asp Net app is it good to have all this static stuff?
Thanks a lot guys!