C#按实体类型自动创建存储库 [英] C# automatic creation of repositories by entity type

查看:74
本文介绍了C#按实体类型自动创建存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了 通用存储库 ,它基于 Entity Framework ,并试图了解如何通过相同的接口和实体类型自动解析存储库。

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;
    }

在这种情况下,我们必须创建一个单独的CategoryRepository-因此,存储库是按类型

in this case we have to create a separate CategoryRepository - so, repository is per type.

这意味着我们最终有很多存储库类。

我想远离多个类,并找到一种变通方法来处理将实体类型作为类型参数传递给接口的存储库

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;
    }

我尝试用Google搜索解决方案,但没有找到很多方法代码示例。

I tried to google the solution but didn't find out much in the way of code examples.

ASP.NET Boilerplate 框架具有功能,如您在来源

我可以

但是它们的实现对我来说有点晦涩,因为似乎有一些额外的代码可以处理自动回购创建

But their implementation is a little obscure to me, as there seems to be some extra code that handles automatic repo creation.

推荐答案

如注释中所述,避免重新发明轮子。如果您使用任何完整的ORM(此处为实体框架),则它本身既可以用作存储库又可以用作UoW。因此,最推荐内联使用ORM本身并绕过Repository和UoW。以下是一些好书:

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 /

但是,在某些情况下,您可能仍想实现存储库。这允许您将存储库注入代码中,从而使代码的其他部分可测试。即使在这种情况下,也请避免使用通用存储库。它被认为是 anti -模式

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.

在这种情况下,请按照聚合根实现具体的存储库。避免使用通用存储库,或者仅将其用作所有具体存储库的基类。可以在 this 答案。

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.

您链接的GitHub示例代码对于Generic Base Repository类而言看起来不错。请注意,它公开了 IQueryable< TEntity>。 GetAll(); 。如果在基类中实现,那应该没问题。切勿从具体存储库中返回 IQueryable 。它违反了存储库模式的基本目的。

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.

这篇关于C#按实体类型自动创建存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆