繁体   English   中英

在.net Core中使用依赖注入

[英]Working with Dependency injection in .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;
        }

将此添加到您的StartUp.cs

services.AddScoped<ICustomerRepository, CustomerRepository>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM