Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将具有通用和具体实现的存储库注入控制器_C#_Generics_Dependency Injection_.net Core_Repository - Fatal编程技术网

C# 将具有通用和具体实现的存储库注入控制器

C# 将具有通用和具体实现的存储库注入控制器,c#,generics,dependency-injection,.net-core,repository,C#,Generics,Dependency Injection,.net Core,Repository,我正在制作一个通用存储库,但对于某些实体,我还需要通用存储库未提供的功能。我有一个接口IGenericRepository,具体实现为带有基本CRUD操作的GenericRepository。此外,我还有一个studentRepository,它使用通用存储库,但也有自己独立于通用存储库的功能,我有一个称为IStudentRepository的接口 以下是示例代码: public interface IGenericEntityRepository<T> {

我正在制作一个通用存储库,但对于某些实体,我还需要通用存储库未提供的功能。我有一个接口IGenericRepository,具体实现为带有基本CRUD操作的GenericRepository。此外,我还有一个studentRepository,它使用通用存储库,但也有自己独立于通用存储库的功能,我有一个称为IStudentRepository的接口

以下是示例代码:

  public interface IGenericEntityRepository<T> 
  {
        Delete(T entity);

        T Get(int id);

        IEnumerable<T> GetAll();

        Add(T entity);

        Update(T entity);
  }


public class GenericEntityRepository<T> : IGenericEntityRepository<T> where T : class
 {

        protected readonly ApplicationDbContext _applicationDbContext;

        public GenericEntityRepository(ApplicationDbContext applicationDbContext)
        {
            this._applicationDbContext = applicationDbContext;
        }

     //Generic Repository Implementations....
 }


 public interface IStudentRepository
 {
      string GetFullName(Student student)
      double GetGpa(Student student)
 }

 public class StudentRepository: GenericRepository<Student>, IStudentRepository
 {
    public StudentRepository(ApplicationDbContext applicationDbContext) : base(applicationDbContext)
    {}

    //IStudentRepository functions' implementations...
 }

Now I need to inject this StudentRepository to my StudentsController 

 public class StudentsController : Controller
 {
        private readonly IGenericEntityRepository<Student> _genericStudentRepository;

        public StudentsController(IGenericEntityRepository<Student> _genericStudentRepository)
        {
            this._genericStudentRepository = genericRepository;           
        }

        public void testAccessibility() 
        {
             this._genericStudentRepository.GetAll() //valid call
             this._genericStudentRepository.GetAllGpa() //invalid Call 
             ***As expected cause IGenericEntityRepository doesn't have that ***function 
        }  

 }
公共接口IGenericEntityRepository
{
删除(T实体);
T Get(int-id);
IEnumerable GetAll();
添加(T实体);
更新(T实体);
}
公共类GenericEntityRepository:IGenericEntityRepository其中T:class
{
受保护的只读ApplicationDbContext\u ApplicationDbContext;
public GenericEntityRepository(ApplicationDbContext ApplicationDbContext)
{
这是。_applicationDbContext=applicationDbContext;
}
//通用存储库实现。。。。
}
公共接口是一种新的存储方式
{
字符串GetFullName(学生)
双GetGpa(学生)
}
公共课堂学生陈述:一般陈述,是学生陈述
{
public StudentRepository(应用程序上下文应用程序上下文):基础(应用程序上下文)
{}
//IStudeEntrepository函数的实现。。。
}
现在我需要将这个StudentRepository注入我的students控制器
公共类学生控制器:控制器
{
私人只读IGenericEntityRepository(U genericstudenrepository);
公立学生控制员(IGenericEntityRepository\U genericStudentRepository)
{
这._genericStudentRepository=genericsepository;
}
公共无效可测试性()
{
这是.\u genericStudentRepository.GetAll()//有效调用
此.\u genericStudentRepository.GetAllGpa()//调用无效
***正如所料,原因IGenericEntityRepository没有***功能
}  
}
正如你在这里看到的问题,如果我注入IGenericEntityRepository,我只会得到一般的Repository功能。如果我希望学生存储库的功能不包括在genericRepository中,我必须像下面一样注入IGenericEntityRepository和IStudentRepository,反之亦然

 public class StudentsController : Controller
 {
        private readonly IGenericEntityRepository<Student> _genericStudentRepository;
        private readonly IStudentRepository _studentsRepository;

        public StudentsController(IGenericEntityRepository<Student> _genericStudentRepository, IStudentRepository studentsRepository)
        {
            this._genericStudentRepository = genericRepository;
            this.__studentsRepository = studentsRepository;
        }

  public void testAccessibility() 
        {
             this._genericStudentRepository.GetAll() //valid call
             this._studentsRepository.GetAllGpa() //valid call 
        }  



 }


公共类学生控制器:控制器
{
私人只读IGenericEntityRepository(U genericstudenrepository);
私人只读是学生档案库(U Students Resposition);;
公立学生控制员(IGenericEntityRepository(通用研究报告报告报告),IStudeRepository studentsRepository)
{
这._genericStudentRepository=genericsepository;
这.uu studentsRepository=studentsRepository;
}
公共无效可测试性()
{
这是.\u genericStudentRepository.GetAll()//有效调用
此.\u studentsRepository.GetAllGpa()//有效调用
}  
}

有更好的方法吗?像这样注入两个上下文相同但编码方式不同的对象是不对的。

您可以使用
IStudentRepository
extend
IGenericEntityRepository

公共接口是udentrepository:IGenericEntityRepository
{
字符串GetFullName(学生)
双GetGpa(学生)
}

现在,注入
IstudeEntrepository
应该足以使用所有函数。

可以工作,但是我已经编写的通用存储库及其基本CRUD函数将在具体存储库中随处复制。@PrajwolCE不,它不会被复制。您已经从GenericRepository继承,这意味着您已经在泛型接口上实现了这些方法。试试看,你就会明白。是的,你是对的,我一直在扩展混凝土studereposition,我不明白为什么它不起作用,而我本应该扩展它的阴暗面!:)非常感谢你,从早上起就一直在拉我的头发!
public interface IStudentRepository : IGenericEntityRepository<Student> 
{
      string GetFullName(Student student)
      double GetGpa(Student student)
}