We want to interact with an instance of the data repository we created in the previous chapter into our API controller. Let's carry out the following steps to do this:
- We'll start by adding using statements to the QuestionsController.cs file so that the data repository and its models can be referenced:
using QandA.Data;
using QandA.Data.Models;
- Create a private class-level variable to hold a reference to our repository:
[Route("api/[controller]")]
[ApiController]
public class QuestionsController : ControllerBase
{
private readonly IDataRepository _dataRepository;
}
We've used the readonly keyword to make sure the variable's reference doesn't change outside the constructor.
- Let's create the constructor as follows:
private readonly IDataRepository _dataRepository;
public QuestionsController()
{
// TODO - set reference to _dataRepository
}
We need to set up the reference to _dataRepository in the constructor. We could try the following:
public QuestionsController()
{
_dataRepository = new DataRepository();
}
However, the DataRepository constructor requires the connection string to be passed in. Recall that we used something called dependency injection in the previous chapter to inject the configuration object into the data repository constructor to give us access to the connection string. Maybe we could use dependency injection to inject the data repository into our API controller? Yes, this is exactly what we are going to do.
- Change the constructor to the following:
public QuestionsController(IDataRepository dataRepository)
{
_dataRepository = dataRepository;
}
So, our constructor now expects the data repository to be passed into the constructor as a parameter. We then simply set our private class-level variable to the data repository passed in.
Unlike the configuration object that was injected into the data repository, the data repository isn't automatically available for dependency injection. ASP.NET Core already sets up the configuration object for dependency injection for us because it is responsible for this class. However, the DataRepository is our class, so we must register this for dependency injection.
- Let's go to startup.cs and add a using statement so that we can reference our data repository:
using QandA.Data;
- Enter the following at the bottom of the ConfigureServices class to make the data repository available for dependency injection:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<IDataRepository, DataRepository>();
}
This tells ASP.NET Core that whenever IDataRepository is referenced in a constructor, substitute an instance of the DataRepository class.
So, if ASP.NET Core encounters a second constructor that references IDataRepository in the same HTTP request, it will use the instance of the DataRepository class it created previously.
So, we now have access to our data repository in our API controller with the help of dependency injection. Next, we are going to implement methods that are going to handle specific HTTP requests.