C# automatic creation of repositories by entity type

落爺英雄遲暮 提交于 2019-12-31 02:44:08

问题


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.


回答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.



来源:https://stackoverflow.com/questions/50867424/c-sharp-automatic-creation-of-repositories-by-entity-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!