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

时间:2019-07-02 07:28:24

标签: c# generics dependency-injection .net-core repository

我正在创建通用存储库,但对于某些实体,我还需要通用存储库未提供的功能。我有一个接口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,那么我只会获得泛型存储库功能。如果我希望学生存储库的功能不包含在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 
        }  



 }


有更好的方法吗?像这样注入两个上下文相同但编码明智的不同对象,感觉不对。

1 个答案:

答案 0 :(得分:3)

您可以将IStudentRepository扩展IGenericEntityRepository<T>

public interface IStudentRepository : IGenericEntityRepository<Student> 
{
      string GetFullName(Student student)
      double GetGpa(Student student)
}

现在注入IStudentRepository应该足以使用所有功能。