This video is available to students only

Creating Specification Evaluator

In this lesson, we're going to write our Specification evaluator

Specification evaluator#

We have created our Specifications. Now we need a Specification evaluator which will take all our queries and expressions and create an IQueryable after aggregating everything together. That IQueryable will contain the list of expressions that we want to provide to our database.

This will be a part of our Infrastructure project. We can create a new class and call it SpecificationEvaluator; this will take a generic type and we will have to create a constraint that says, where T is a class. Inside the class, we will create a public static method; static method because they can be used without creating a new instance of the class.

This method will return an IQueryable of type T. We will call this method GetQuery. This method will accept two parameters. One of them will be an IQueryable of type t — we can call it inputQuery — and the second one will be an ISpecification of type T which we can simply call spec. Let's import IQueryable from System.Linq and ISpecification from Entity.Specification.

We need a variable to store our query, so we can write query = inputQuery. Now I want to check what's inside our specification. If we have spec.Criteria, we can check it by writing if spec.Criteria is not equal to null, query will be equal to query.Where(spec.Criteria). If you're not aware of the where statement, it works like include but it takes an expression which should return a boolean; for example, we can use this statement to check the courses below $10.

Now I want to check my Include, so I can write query = spec.Include.Aggregate because our Include statement will be more than one. Just like we have in our CourseRepository, we're just accumulating them by using Aggregate. Inside Aggregate, first part we will pass is a query. Second part will accept two parameters; one is current, which is the Current entity and the other one will be include which is the expression of our Include statement.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React