This video is available to students only

Adding Repository and Interface

In this lesson, we're going to add Repository and Interface to our Project

Adding repository and interface#

In the last lecture, we saw what Repository pattern is. It's now time to implement it. First thing we want to do is to create an interface for our repository. An interface creates a contract with the actual repository which means our repository needs to follow everything our interface says.

Our Entity project will contain all our interfaces and our Infrastructure project will be responsible for implementing them. Let's create another directory inside Entity project and let's call it Interfaces. In C#, it's a convention to name the interfaces by prefixing with I, so our interface will be called ICourseRepository.

Inside our interface, we have two methods right now; let's create them. Whenever we are doing something asynchronously, we use Task class which represents some work that should be done. The task can tell you if the work is completed, and if the operation returns a result, the task gives you the result. In the first method, we want to return a course so we can write Task of type Course followed by the name of the method which is going to be GetCourseByIdAsync which will accept an id that will be an integer. Since we're using Task, it needs to be imported from System.Threading.Tasks.

Our second method will return the list of courses so we can write Task of IReadOnlyList. List will be of type Course and the name of the method will be GetCoursesAsync(). We need to import IReadOnlyList from System.Collections.Generic. Simply put a cursor there and enter cmd and . on Mac and ctrl and . on Windows, and click on import from System.Collections.Generic. IReadOnlyList needs to be used when the sole purpose is to show the result and not mutation of it. Our interface is now ready, we just need to create a class which implements it.


Start a new discussion. All notification go to the author.