c# - 在 .net 核心中使用依赖注入
问题描述
我正在尝试以下列方式使用依赖注入,但是我遇到了错误
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();
}
}
客户.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; }
}
启动.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<>));
}
客户控制器.cs
private readonly ICustomerRepository custRepository;
public ValuesController(IOptions<DBConfiguration> dBConfiguration, ICustomerRepository _custRepository)
{
custRepository= _custRepository;
}
解决方案
将此添加到您的 StartUp.cs
services.AddScoped<ICustomerRepository, CustomerRepository>();
推荐阅读
- node.js - Express、i18n 和自定义未找到页面
- r - 在循环中在 ggplot 中按名称引用列
- html - 最大高度:适合内容;在 Firefox 中无法正常工作
- javascript - js-render if 构造带参数
- react-native - jest 在测试 react-native 组件时找不到 haste 模块
- mysql - 尝试使用存储过程在 MySQL 中创建 PIVOT,不会添加只有空记录的列
- json - 根据发布对用户故事进行分组
- angular - Angular 4 ng-bootstrap close 子组件的模态
- angularjs - 使用AngularJS在搜索栏上的动态关闭按钮
- c# - C#中动态创建数组的方法