I found an example of Generic Repository which is based on Entity Framework and trying to understand how to automatically resolve repositories by the same interface and entity type.
The link above leads to the repo where you can see the following approach:
public class HomeController : Controller
{
private readonly ICategoryRepository _repository;
public HomeController(ICategoryRepository repository)
{
_repository = repository;
}
in this case we have to create a separate CategoryRepository - so, repository is per type.
That means we end up having a lot of classes for repositories.
I would like to stay away from multiple classes and find a workaround to work with repositories passing entity type to interface as type parameter
public class HomeController : Controller
{
private readonly IRepository<Category> _repository;
public HomeController(IRepository<Category> repository)
{
_repository = repository;
}
I tried to google the solution but didn't find out much in the way of code examples.
ASP.NET Boilerplate framework has this functionality as you can see from sources.
I can see the interface files in that folder and they make sense.
But their implementation is a little obscure to me, as there seems to be some extra code that handles automatic repo creation.