Creating the repository class

Let's create a class to hold all of the methods for interacting with the database:

  1. In the Solution Explorer, right-click on the project, select the Add menu, and then choose the New Folder option.
  2. A new folder will be created in the solution tree. Name the folder Data.
  3. Right-click on the Data folder and select the Add menu and then choose the Class... option.
  4. In the dialog box that appears, enter DataRepository for the name of the file to create and click the Add button.

 

  1. A skeleton DataRepository class will be created:

  1. We are going to create an interface for the data repository so that it can be mocked when writing unit tests. Right-click on the Data folder and select the Add menu and then choose the Class... option.
  2. This time, choose the Interface option in the dialog box that appears and name it IDataRepository before pressing the Add button.
  3. Change the modifier for the interface to be public and add the following methods:
public interface IDataRepository
{
IEnumerable<QuestionGetManyResponse> GetQuestions();

IEnumerable<QuestionGetManyResponse>
GetQuestionsBySearch(string search);

IEnumerable<QuestionGetManyResponse>
GetUnansweredQuestions();

QuestionGetSingleResponse
GetQuestion(int questionId);

bool QuestionExists(int questionId);

AnswerGetResponse GetAnswer(int answerId);
}

So, we are going to have six methods in the data repository to read different bits of data from our database. Note that this won't compile yet because we are referencing classes that don't exist.

  1. Moving back to DataRepository.cs, specify that the class must implement the interface we just created:
public class DataRepository: IDataRepository
{
}
  1. If we click on the class name, a light bulb icon will appear. Click on the light bulb menu and choose Implement interface:

Skeleton methods will be added to the repository class that satisfies the interface.

  1. Create a class-level private variable called _connectionString to store the database connection string:
public class DataRepository : IDataRepository
{
private readonly string _connectionString;
...
}

The readonly keyword prevents the variable from being changed outside of the class constructor, which is what we want in this case.
  1. Let's create a constructor for the repository class that will set the value of the connection string from the appsettings.json file:
public class DataRepository : IDataRepository
{
private readonly string _connectionString;

public DataRepository(IConfiguration configuration)
{
_connectionString =
configuration["ConnectionStrings:DefaultConnection"];

}

...
}

The configuration parameter in the constructor gives us access to items within the appsettings.json file. The key we use when accessing the configuration object is the path to the item we want from the appsettings.json file with colons being used to navigate fields in the JSON.

How does the configuration parameter get passed into the constructor? The answer is dependency injection, which we'll cover in the next chapter.

  1. Our class doesn't recognize IConfiguration yet, so, let's click on it, click on the light bulb menu that appears, and choose using Microsoft.Extensions.Configuration:

We've made a good start on the repository class. We do have compile errors, but these will disappear as we fully implement the methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.87.138