How-to implement Specification Pattern with Entity Framework ?
As probably you already know, the specification pattern will enable you to send a filter to your repository (among other usages). I have seen many implementations to do that.
Usually, people expose another method on the specification interface representing the expression tree that has to be sent to Entity Framework:
public interface ISpecification
{
bool IsSpecifiedBy(T item);
Expression> GetPredicate()
}
The repository will call the GetPredicate
method and pass it to the Where
method on EF´s DbSet.
That way you have restricted which expressions will be generated, and guarantee that it´ll generate a valid SQL statement.
To enable the boolean operators on the specification, you´ll need to mix expressions together. there is this post from Vladmir Khorikov where he explains in detail how to do that.
I usually dont like this solution, since it assumes your domain model is the same as your persistence model. Most people are ok with that. But I like to keep things VERY separated on a Onion architecture.
I found by experience that eventually Entity Framework will pollute your domain model with dbcontexts, EF attributes, public setters, properties that only make sense on the database, etc.
So I usually keep 2 separate models (classes), with the "persistence" entity one being very simple and very similar to the database schema, and a "domain" entity enriched with behavior and invariants.
And that poses a problem to the solution above, because the specification lives on the domain model, and cannot have dependencies for the persistence model.
So you´ll need to navigate the specification composite and create to create a predicate. the Visitor is a good design pattern for that.
I recently wrote a series of posts where I explain