开发者

Specification pattern vs Extension method?

I am trying to grasp specification pattern and i get confused a little about it. I really couldn't found it helpful for my specific requirements. I want to know that what is problem if i prefer extension methods for my complex spesifications? For example

public static class ProductExtensions
{
    public static IQueryable<Product> InStocks(this IQueryable<Product> query)
    {
        return query.Wh开发者_开发技巧ere(p => p.InStock && !p.IsDeleted /*others goes here*/);
    }
}

I found it helpful to wrap my long specifications with an extension methods instead of using spesification pattern. What is wrong with this ?


There's nothing wrong with your current approach.

Specification pattern as a very generic concept makes sense when you are dealing with combinations that can be arbitrarily applied because they express orthogonal concepts - i.e. the product is a microwave and it also weighs less than 5 pounds.

Extension methods makes sense when you want to group certain conditions that always appear together i.e. the product is in stock and we are still offering it to form an easier to use abstraction i.e. InStock. Other uses of extension methods are to allow a more "fluid" composition of your final query, which many prefer.

Both concepts are not mutually exclusive and you should use what results in the most readable code for whatever you are trying to express.


Whereas the specification pattern focuses on building up a list of criteria via method chaining and then checking a single object against that criterion, LINQ tends to focus on building up a transformation query via method chaining. The transformation can include criterion (Where) links, but other links are possible too.

I don't see any reason to think that the specification pattern is "better" than the patterns established by the LINQ framework, so your approach isn't necessarily "wrong." However, keep in mind that your method will only work on IQueryable objects, and not on IEnumerable objects. This can cause limitations when it comes to using technologies like LINQ to Entities, which don't know how to translate .InStocks() into a SQL statement.


Specification pattern allows the programmer to compose data at runtime. This would be more flexible than just using extension methods. For example, what if you wanted an IQueryable<Product> of products that are in stock AND all products that either sold out last week or have never been ordered? Composing a query like this would require you to create a whole other extension method, where the specification pattern would allow you to compose it using the simple API.

The IQueryable interface actually mitigates some of the advantages of the specification pattern, but in my opinion it's not always clear to the developer exactly how and where queries will be executed with IQueryable, which may or may not be a problem depending on your circumstances.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜