I am building a WarehouseApi, but when I try to inject my service it gives me an error(CS0119)
public static IServiceCollection AddServices(this IServiceCollection services)
{
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped(IEmployeeRepository, EmployeeRepository); //CS0119
return services;
}
Error descriptions: EmployeeRepository' is a type, which is not valid in the given context and
IEmployeeRepository' is a type, which is not valid in the given context
Here are IEmployeeRepository and EmployeeRepository:
public interface IEmployeeRepository : IRepository<Employee>
{
Employee GetFirstOrDefaultByFirstName(string firstNameToFind);
Employee GetFirstOrDefaultByLastName(string lastNameToFind);
Employee GetFirstOrDefaultbByAge(int ageToFind);
Employee GetByEmail(string emailToFind);
Employee GetByAddress(string addressToFind);
Employee GetByPhoneNumber(string phoneNumberToFind);
}
public class EmployeeRepository : Repository<Employee>, IEmployeeRepository
{
public EmployeeRepository(WareHouseDBContext empContext) : base(empContext) { }
public Employee GetByAddress(string addressToFind)
{
return context.Set<Employee>().FirstOrDefault(x => x.Address == addressToFind);
}
public Employee GetByEmail(string emailToFind)
{
return context.Set<Employee>().FirstOrDefault(x => x.Email == emailToFind);
}
public Employee GetByPhoneNumber(string phoneNumberToFind)
{
return context.Set<Employee>().FirstOrDefault(x => x.PhoneNumber == phoneNumberToFind);
}
public Employee GetFirstOrDefaultbByAge(int ageToFind)
{
return context.Set<Employee>().FirstOrDefault(x => x.Age == ageToFind);
}
public Employee GetFirstOrDefaultByFirstName(string firstNameToFind)
{
return context.Set<Employee>().FirstOrDefault(x => x.FirstName == firstNameToFind);
}
public Employee GetFirstOrDefaultByLastName(string lastNameToFind)
{
return context.Set<Employee>().FirstOrDefault(x => x.LastName == lastNameToFind);
}
}
I'm new to APIs, Dependency Injection, etc, so this might be an easy fix
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…