【问题标题】:Working with Dependency injection in .net core在 .net 核心中使用依赖注入
【发布时间】:2019-07-18 23:26:25
【问题描述】:

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

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;
        }

【问题讨论】:

  • 您正在注入ICustomerRepository,容器对此一无所知。因此错误。仅仅因为您注册了通用接口并不意味着它会知道派生接口及其实现。
  • @Nkosi 同意,但是如何实现呢?
  • 有人已经提供了有效的答案
  • @Nkosi 是的,但我不这么看
  • 人们为完全不必要的事情而经历的圈套。只需不使用存储库,您就可以为自己节省大量时间、精力和精力。 DbContext 是您的工作单元,每个 DbSet 是一个存储库。当您使用像 EF 这样的 ORM 时,您只是选择使用第三方 DAL。为什么在天堂里人们认为他们需要将自己的 DAL 包裹起来,这超出了我的范围。通常,有人会插话:抽象 EF 依赖项。真的吗?您的存储库直接依赖于它,您甚至将其泄漏到应用程序Startup 中。对我来说似乎不是很“抽象”。

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


【解决方案1】:

将此添加到您的 StartUp.cs

services.AddScoped<ICustomerRepository, CustomerRepository>();

【讨论】:

  • 不,我不想那样做...这将导致在 startup.cs 文件中添加存储库的数量。我想以通用方式实现
  • @XamDev 然后按照惯例进行注册。您可以选择将所有具体类型注册为它们实现的接口,或者您可以添加一个标记接口以仅对某些类型执行此操作。
  • This blog entry 展示了如何使用 Scrutor 通过 Microsoft.Extensions.DependencyInjection 来实现。
猜你喜欢
  • 2018-12-14
  • 1970-01-01
  • 2019-02-16
  • 2021-03-02
  • 2020-01-17
  • 1970-01-01
  • 2017-12-09
  • 2016-12-07
  • 2018-01-03
相关资源
最近更新 更多