Injecting the data repository into the API controller

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:

  1. 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;
  1. 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.

  1. 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.

Dependency injection is the process of injecting an instance of a class into another object. The goal of dependency injection is to decouple a class from its dependencies so that the dependencies can be changed without changing the class. ASP.NET Core has its own dependency injection facility that allows class dependencies to be defined when the app starts up. These dependencies are then available to be injected into other class constructors.
  1. 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.

  1. Let's go to startup.cs and add a using statement so that we can reference our data repository:
using QandA.Data;

  1. 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.

The AddScoped method means that only one instance of the DataRepository class is created in a given HTTP request. This means the lifetime of the class that is created lasts for the whole HTTP request.

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.

As well as AddScoped, there are other methods for registering dependencies that result in different lifetimes for the generated class. AddTransient will generate a new instance of the class each time it is requested. AddSingleton will generate only one class instance for the lifetime of the whole app.

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.

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

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