开发者

Static business tier and extension methods

开发者 https://www.devze.com 2023-02-24 04:01 出处:网络
I\'ve just discovered extension methods and I love th开发者_Go百科em so much that I\'m scared about their good use...

I've just discovered extension methods and I love th开发者_Go百科em 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:

  1. Do you think this is a good approach to an n-tier app?, is this a good design pattern or just a lazy solution?

  2. 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())

  3. Being Asp Net app is it good to have all this static stuff?

Thanks a lot guys!


It is quite common to use custom extension methods on top of IQueryable. I think it is a good way to reuse query parts. Using extension methods should be done with caution. Extension method is just a syntactic sugar around static method. My friend have a nice story about his collegue who recently discovered extension methods and started to use code like:

var order = 1.GetOrder(); // Extension on integer - this is really bad example

The problem in your example is static business layer. Business layer must receive context somehow and context must be handled per request. Sharing context among requests or having single context for whole application is the best way to have serious troubles. So unless you pass context to each method you must create business class per request as well.

But still you should use extensions on IQueryalbe. You will probably find out that your business class is just a wrapper around context.Query and in such case it is additional layer which is not needed.

Also be aware that this all works in layered architecture but if your n-tier means real physical separation (each tier on a separate server) then it is a different story because you need something which would allow you to creatinh a linq query on one tier and executing it on another tier (on another server so the query and the result must be transported over the network) - one such tool are WCF Data Services.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号