簡體   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