Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.Where(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 ?

like image 904
Freshblood Avatar asked Mar 31 '11 20:03

Freshblood


People also ask

What is the main purpose of an extension method?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What is extension method with example?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.

Are extension methods good?

And static methods and properties and methods are not thread safe and therefore should be avoided then extension methods and extension properties are bad. We are just tricked to do those because the codes we write will appear as pretty or clean but performance-wise it is not.


2 Answers

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.

like image 71
BrokenGlass Avatar answered Sep 17 '22 22:09

BrokenGlass


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.

like image 22
StriplingWarrior Avatar answered Sep 19 '22 22:09

StriplingWarrior