gpt4 book ai didi

c# - 将具有通用和具体实现的存储库注入(inject) Controller

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:22 24 4
gpt4 key购买 nike

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

示例代码如下:

  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
}

}

正如您在这里看到的问题,如果我注入(inject) IGenericEntityRepository,我只会获得 genericrepository 功能。如果我想要不包含在 genericRepository 中的 Student 存储库的功能,我必须像下面一样注入(inject) 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
}



}


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

最佳答案

你可以有IStudentRepository扩展 IGenericEntityRepository<T> :

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

正在注入(inject) IStudentRepository应该足以使用所有功能。

关于c# - 将具有通用和具体实现的存储库注入(inject) Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56847378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com