Specification Pattern Walkthrough
In this lesson, we're going to walk through the entire process of Specification pattern
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
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 - 01:14] Although we might say we haven't made any progress in terms of what we have achieved as a result, we still have two repositories which retrieve the same data from the backend. But if you see it from a perspective of a big project, you have set up the foundation by creating generic repository and the specifications. Anyways purpose of this video is to take you through the journey from getting a request till sending the data back to the user. Let's pick an example, go to our courses controller and look at the get cause method. So we receive a request, we pass the ID to our courses with categories specification. Now let's coincide our costs with categories specification implements this base specification and here we have two constructors. If you go inside our base specification, we have an empty constructor and we have a constructor with criteria. We are using the empty constructor for our list methods and the criteria one in the get by ID method. Here we are passing ID to our expression and inside the base we are using our expression. If you look at our base specification, this is the base method where we are passing this expression.
[01:15 - 03:23] Below that we have used include method which takes requirements and learnings expressions. Let's check that out as well. It takes an expression and adds it to our include list. So our spec here has an entity with ID and learnings and the requirements. Now we pass that spec to our get entity with spec method. Let's check that out as well. I'll go to the generic repository and find the get entity with spec method here. It takes a specification which we have looked at right now. First of all it uses the apply spec method which uses this get query method. Get query method takes an i-queryable and a specification. If you go to this method, here we are checking if our spec has a criteria. If it has, we are using the where statement with that expression. So here we are basically passing. Let's go to the courses with categories specification. This expression . This expression is basically being used here. Below that we are aggregating our include statements. Since we have used two, our include is a list of two expressions. One is requirements, another one is the learnings. Finally here we are returning the query which has our i-queryable with all these specifications and include statements. Let's go to our course repository. Let me see that. This is the query here and if we compare it to our generic repository method, this part is basically equivalent to this. The only difference is the ID is passed before making the query to the database. Finally we use first or default async to make the database query. Let's go back to our courses controller.
[03:24 - 03:56] This is what our get entity with spec method does. Our course variable has all the data. We finally map our course to cost.io which I hope you clearly understand. We map them inside the mapping profiles. Here where we are mapping course to course. Well this is the whole setup. A little overwhelming I understand but once you're comfortable with it, it will be easier to make bigger projects.