Creating Specification Evaluator
In this lesson, we're going to write our Specification evaluator
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.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 00:29] We have created our specifications. Now we need a specification evaluator which will take all our queries and expressions and will create an i queryable after aggregating everything together. That i queryable will contain list of expressions that we want to provide to our database. This will be a part of our infrastructure project. So let's create a new class and let's call it specification evaluator.
[00:30 - 00:57] This will take a generic type T and we will have to create a constraint that says that type T should be a class. Inside the class we will create a static method. So let's write public static. We are using the static method because they can be used without creating a new instance of the class.
[00:58 - 01:15] This method will return an i queryable. So let me write i queryable which will be of type T and we will call this get query. So I will write get query.
[01:16 - 01:48] This method will accept two parameters. One of them will be an i queryable of type T. So I can write i variable of type T. We will give it a name input query and the second parameter will be an i specification of type T. So I will write i specification of type T and let's simply call it spec.
[01:49 - 02:09] Let's import i queryable using system.link and let's import i specification using entity.specifications. We need a variable to store our query. So what we can do is we can create a new variable query and this will be the input query.
[02:10 - 02:35] Now I want to check what's inside our specification. So we can write if spec.criteria is not equal to null. So this is basically checking if there is a criteria inside our spec and this time query will be query.where.
[02:36 - 02:55] Since spec.criteria is not null we can pass spec.criteria inside. If you are 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 the statement to check the courses below $10.
[02:56 - 03:13] So basically where takes an expression which returns true or false. Now I also want to check my include. So what I can do is outside the if statement I can write query is equal to spec.include.
[03:14 - 03:28] Now I will use aggregate because our include statement will be more than one just like we had in our repository, right? We have more than one include statement. That's why we have to use the aggregate to aggregate all of them together.
[03:29 - 03:37] In the first part we will pass query. Second part will accept two parameters. So I can create brackets.
[03:38 - 03:58] First one is current which is the current entity and the second one will be include which is the expression of our include statement. So I can write include. Now what we will do is we will write current.include. And inside we will pass this include.
[03:59 - 04:15] And the spelling should be CU double R and here as well. We have to import include from entity framework call and finally we will return this query.
[04:16 - 04:32] And that's simply it. This method is doing the exact same thing as our repositories. It might look a little complicated, but the purpose is exactly the same. We are using it for our generic repositories.
[04:33 - 04:43] This query that we are returning is basically an I queryable, which will go to our methods. Also, this is the aggregate of whatever queries we are making to the database.
[04:44 - 05:00] And now if we go to our generic repository interface, so yeah, here both of our methods don't accept any specification parameter. So what we'll have to do is create two new methods which will accept our specification parameters.
[05:01 - 05:25] The first one will be again task of type T. We can call it get entity with spec and this will accept I specification of type T as a parameter. So we can write I specification of type T and we can call it spec.
[05:26 - 05:43] We will have to import I specification using entity dot specifications. Our next method will return I read only list of type T. So what we'll do is task of I read only list of type T.
[05:44 - 05:58] We can call it list with spec list with spec. This method will accept an I specification as well. So I specification of type T.
[05:59 - 06:11] We can call it spec again. No issues there. And that's simply it. This might look very complicated to you right now, but trust me once we start implementing it, things will get much simpler.
[06:12 - 06:22] So right now we can see our generic repository is giving us errors because we haven't implemented these two methods. So let's see how to implement these in our next lecture.