2

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.

Alex Herman
  • 2,708
  • 4
  • 32
  • 53
  • 2
    If you are using Entity Framework (given the tag), consider just using it directly and forget about the generic repository stuff. You already have it, it's the `DbSet` class. – Camilo Terevinto Jun 14 '18 at 23:54
  • 1
    Why reinvent the wheel? You already using entity framework. It is already a generic repository with UoW implementation. – Christian Gollhardt Jun 14 '18 at 23:54
  • Are you asking how to creating a generic class? https://www.dotnetperls.com/generic – SILENT Jun 15 '18 at 00:00
  • @CamiloTerevinto very interesting, makes more sense to take the advantage of EF then. thank you very much indeed! – Alex Herman Jun 15 '18 at 00:19
  • @AlexHerman should take some time a review the following on the topic http://www.weeklydevtips.com/024 it is very interesting. – Nkosi Jun 15 '18 at 04:14

1 Answers1

1

As mentioned in the comments, avoid reinventing the wheel. If you are using any full ORM (the Entity Framework here), it itself serves as Repository as well as UoW. So, using ORM itself inline and bypassing both Repository and UoW is most recommended. Following are some good read:

https://ayende.com/blog/4784/architecting-in-the-pit-of-doom-the-evils-of-the-repository-abstraction-layer

http://www.primaryobjects.com/2010/03/17/using-the-nhibernate-repository-pattern-in-c-asp-net/

But, in some cases, you may still want to implement Repository. This allows you injecting the repository in your code making other part of the code testable. Even in that case, avoid using Generic Repository. It is considered an anti-pattern.

In that case, implement concrete repositories per Aggregate Root. Avoid Generic Repository OR just use it as base class for all your concrete repositories. More explanation could be found in this and this and this answers.

Your linked GitHub sample code looks good interface for Generic Base Repository class. Note that it exposes IQueryable<TEntity> GetAll();. If implemented in base class, this should be fine. Never return IQueryable from your concrete repositories. It violates basic purpose of Repository Pattern.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141