C# automatic creation of repositories by entity type

后端 未结 1 1870
囚心锁ツ
囚心锁ツ 2021-01-22 18:10

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

相关标签:
1条回答
  • 2021-01-22 18:15

    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.

    0 讨论(0)
提交回复
热议问题