在.net Core中使用依赖注入

时间:2019-07-18 15:22:12

标签: c# asp.net-core dependency-injection .net-core

我正在尝试通过以下方式使用依赖注入,但是我遇到了错误

  

InvalidOperationException:无法解析类型的服务   ”,同时尝试激活”。

我想要实现的是,我已经在通用接口中定义了一些通用方法,而在其他方法中,数据驱动逻辑会在特定于存储库的特定接口(如ICustomerRepository)中写下来。

并从控制器中调用这些常见和特定的方法。但是如上所述,尝试执行API时出现上述错误。

之所以要保留通用存储库,是因为不应该在每个存储库中添加通用存储库时将其添加到StartUp.cs中。

那么,如何通过存储库特定的接口执行通用和非通用方法?

对此有任何帮助!

下面是我的代码

IGenericRepository.cs

public interface IGenericRepository<TEntity> where TEntity : class
    {
        IEnumerable<TEntity> GetAll();
        TEntity GetById(int id);
        void Create(TEntity entity);
        Task Update(int id, TEntity entity);
        TEntity Delete(int id);
    }

GenericRepository.cs

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    private readonly DbContext dbContext;

    public GenericRepository(DbContext dbContext)
    {
        this.dbContext = dbContext;
    }

    public void Create(TEntity entity)
    {
        dbContext.Set<TEntity>().Add(entity);
        dbContext.SaveChanges();
    }

    public TEntity Delete(int id)
    {
        throw new NotImplementedException();
    }

    public IEnumerable<TEntity> GetAll()
    {
        return dbContext.Set<TEntity>().ToList();
    }

    public TEntity GetById(int id)
    {
        return dbContext.Set<TEntity>().Find(id);
    }

    public Task Update(int id, TEntity entity)
    {
        throw new NotImplementedException();
    }

    IEnumerable<TEntity> IGenericRepository<TEntity>.GetAll()
    {
        return dbContext.Set<TEntity>().ToList();
    }
}

ICustomerRepository.cs

public interface ICustomerRepository : IGenericRepository<Customer>
    {
        Task<Customer> GetCustomerDetails();
    }

CustomerRepository.cs

 public class CustomerRepository : GenericRepository<Customer>, ICustomerRepository
    {
        private readonly BaseDBContext _dbContext;

        public CustomerRepository(BaseDBContext dbContext)
            : base(dbContext)
        {
            _dbContext = dbContext;
        }

        public Task<Customer> GetCustomerDetails()
        {
            throw new NotImplementedException();
        }
    }

Customer.cs

 public class Customer
    {
        [Key]
        public int CustId { get; set; }
        public string CustomerName { get; set; }
        public string City { get; set; }
        public string Designation { get; set; }
    }

StartUp.cs

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFrameworkSqlServer().AddDbContext<BaseDBContext>();

            services.Configure<DBConfiguration>(Configuration.GetSection("DBConfiguration"));

            services.AddScoped<DbContext, BaseDBContext>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
        }

CustomerController.cs

private readonly ICustomerRepository custRepository;

        public ValuesController(IOptions<DBConfiguration> dBConfiguration, ICustomerRepository _custRepository)
        {
            custRepository= _custRepository;
        }

1 个答案:

答案 0 :(得分:3)

将此添加到您的StartUp.cs

services.AddScoped<ICustomerRepository, CustomerRepository>();