Entity Framework Specification Template Implementation

How to implement a specification template with Entity Framework?

+17
c # design-patterns entity-framework
Feb 28 '10 at 21:30
source share
4 answers
  • Specification:
    For those who want a tutorial, visit the link .

  • Understand the specification for the Entity platform:
    Read this . This applies to the following very important points. In any real-world application, you quickly want to combine several specifications together. This is called a specification set. You will need to understand some of the caveats to enable Linq composition specifications in Entities. You should know this because using Linq to Entities is a desirable approach to expressing criteria for spec satisfaction.

  • Correct the error:
    Download and install this . It fixes the Linq flaw for the entities that you read about in the second step. This explains more details about the fix.

  • Implement it!
    You should have enough information to implement the template. Keep googling. Doing this for EF is not entirely easy, but worth it. This one is a very interesting implementation.

+17
Jan 27 '11 at 10:26
source share

In principle, when implementing a specification template, there should be nothing special (due to EF). You implement specifications as separate classes that work with your domain model.

You can find a lot of articles or webcasts about the spec template and even some that use EF, like here and here .

+4
Feb 28 '10 at 21:45
source share

Just use NSpecification lib. It's free. You can use it with any ORM based on the IQueryable interface, such as Entity Framework or Linq2Sql: https://github.com/jnicolau/NSpecifications

Or get it from Nuget:

Package Installation Specifications -Version 1.1.0

+1
Mar 01 '19 at 15:03
source share

As you probably already know, the specification template allows you to send a filter to your repository (among other cases). I have seen many implementations for this.

Usually, people provide another method in the specification interface that represents the expression tree that should be sent to the Entity Framework:

public interface ISpecification<T> { bool IsSpecifiedBy(T item); Expression<Func<T, bool>> GetPredicate() } 

The GetPredicate repository GetPredicate method and passes it to the Where method in DbSet EF. In this way, you have limited what expressions will be generated, and you guarantee that it will generate a valid SQL statement.

To include logical operators in the specification, you need to mix the expressions together. There is this post from Vladimir Khorikov, where he explains in detail how to do this.

I usually don’t like this solution because it assumes that your domain model matches the persistence model . Most people are okay with that. But I like to keep things VERY separated onion architecture .

From my own experience, I found that eventually the Entity Framework will pollute your domain model with dbcontexts, EF attributes, open setters, properties that make sense only for the database, etc.

Therefore, I usually keep two separate models (classes), the essence of "constancy" is very simple and very similar to the database schema, and the essence of the "domain" is enriched by behavior and invariants.

And this creates a problem for the solution described above, since the specification is based on the domain model and cannot have dependencies for the constancy model.

Therefore, you will need to navigate through the composite specification and create it to create the predicate. Visitor is a good design template for this.

I recently wrote a series of posts where I explain

0
Apr 05 '18 at 1:20
source share



All Articles