Implementing Specification Methods
In this lesson, we're going to implement the Specification methods
We have just created two more methods that take the specification. It's now time to implement them in our actual Generic Repository. Let's open Generic Repository and use quick fix to implement interface. This will add two of our new methods and will get rid of the error.
First thing we need to do inside our Generic Repository is to create a method which will let us apply these specifications, so let's create a private method which will return an IQueryable of generic type T; we can call it ApplySpec. It needs an ISpecification as a parameter, so we can write ISpecification of type T; let's call it spec again. Import IQueryable from System.Linq.
Now let's write our method. If you remember, we have created GetQuery method inside Specification Evaluator class; let's use it here. This will take IQueryable of type T as the first parameter and ISpecification as the second one. We want to make our entity queryable. For example, if we go to the course repository, we are making Course Entity queryable, so we need to pass the entity as our first parameter. Since we are inside Generic repository, we want to make it dynamic, so what we can do is write \_context.Set T()
and we will make it queryable by writing as queryable. In the second parameter, we can simply pass the spec.
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.
