Here's the new source code with the new DI-injected instances (new/updated lines are highlighted):
using System;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using TestMakerFreeWebApp.Data;
using Mapster;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
namespace TestMakerFreeWebApp.Controllers
{
[Route("api/[controller]")]
public class BaseApiController : Controller
{
#region Constructor
public BaseApiController(
ApplicationDbContext context,
RoleManager<IdentityRole> roleManager,
UserManager<ApplicationUser> userManager,
IConfiguration configuration
)
{
// Instantiate the required classes through DI
DbContext = context;
RoleManager = roleManager;
UserManager = userManager;
Configuration = configuration;
// Instantiate a single JsonSerializerSettings object
// that can be reused multiple times.
JsonSettings = new JsonSerializerSettings()
{
Formatting = Formatting.Indented
};
}
#endregion
#region Shared Properties
protected ApplicationDbContext DbContext { get; private set; }
protected RoleManager<IdentityRole> RoleManager { get; private
set; }
protected UserManager<ApplicationUser> UserManager { get;
private set; }
protected IConfiguration Configuration { get; private set; }
protected JsonSerializerSettings JsonSettings { get; private
set; }
#endregion
}
}
After all the DI we've been through, we can easily understand what we did here, so let's go ahead.