首先注册需要注入的所有内容:
// You can lazily create the IMapper, or if you prefer, use
// a concrete value.
services.AddSingleton<IMapper>(sp => mappingConfig.CreateMapper());
// Make your EmployeesRepo implement IEmployeesRepo
services.AddTransient<IEmployeesRepo, EmployeesRepo>();
// I'm guessing but you'll have the connection string somewhere like this.
// Also using a DbContextPool instead has benefits (though not required)
services.AddDbContextPool<EFContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Data")));
IEmployeesRepo _repo;
public EmployeesController(IEmployeesRepo repo)
{
_repo = repo;
}
private readonly EFContext _db;
private readonly IMapper _mapper;
// Assign the object in the constructor for dependency injection
public EmployeesRepo(EFContext db, IMapper mapper)
{
_db = db;
_mapper = mapper;
}