简体   繁体   English

通用存储库问题

[英]Generic Repository questions

I'm developing a generic repository for my application and I'm having a few doubts here. 我正在为我的应用程序开发一个通用存储库,我在这里有一些疑问。

This is some of the code I have for the generic repository: 这是我对通用存储库的一些代码:

public interface IEntityRepository<T> where T : class
{
    void Add(T entity);
    T GetById(int id);
    IEnumerable<T> Get(Expression<Func<T, bool>> predicate);
    IEnumerable<T> GetAll();
}

public class EntityRepository<T> : IDisposable, IEntityRepository<T> where T : class
{
    protected DbSet<T> DbSet;
    protected DbContext Context;

    public EntityRepository(DbContext dataContext)
    {
        DbSet = dataContext.Set<T>();
        Context = dataContext;
    }

    public void Add(T entity)
    {
        DbSet.Add(entity);
    }

    public IEnumerable<T> Get(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Where(predicate);
    }

    public IEnumerable<T> GetAll()
    {
        return DbSet;
    }

    public T GetById(int id)
    {
        return DbSet.Find(id);
    }

    // IDisposable
    public void Dispose()
    {
        if (Context != null)
        {
            Context.Dispose();
        }
        GC.SuppressFinalize(this);
    }
}

What I'm having difficulties with is this: 我遇到的困难是:

1 - should I be returning IEnumerable from the repository layer to the services layer instead of IQueryable? 1 - 我应该将IEnumerable从存储库层返回到服务层而不是IQueryable吗? I've read some articles on the web on this topic but couldn't find a definitive - or reasonably definitive - answer to this question. 我已经在网上阅读了一些关于这个主题的文章,但是找不到这个问题的确定性或合理确定的答案。 By returning IEnumerable all subsequent queries will be done locally right? 通过返回IEnumerable,所有后续查询都将在本地完成吗?

2 - One thing that will obviously be needed is to be able to retrieve paged data. 2 - 显然需要的一件事是能够检索分页数据。 I don't want to get 100,000 records just to show 50. My question is if this "logic" should be on the repository or in the service, ie, the service gets all the data and then skips/takes however it needs or the repository already returns only the data that the service will need? 我不想只显示50条记录。我的问题是这个“逻辑”是否应该在存储库或服务中,即服务获取所有数据,然后跳过/接受它需要或者存储库已经只返回服务需要的数据? Or should these types of methods be in the a specific repository that inherits the generic one? 或者这些类型的方法是否应该在继承泛型方法的特定存储库中?

Thanks in advance 提前致谢

Never return IQueryable since it's an implementation detail of an ORM and defeats the spirit of using the Repository: tell the Repo what you want from it, not how . 永远不会返回IQueryable的,因为它是一个ORM的实现细节违背了使用存储库的精神:告诉你回购从它想要什么 ,而不是如何 IQueryable means you are building part of the query yourself thus you tell it how . IQueryable意味着你自己构建查询的一部分,因此你告诉它如何

The generic repository is mostly an anti-pattern, as it uses the ORM entities instead of Domain entities . 通用存储库主要是反模式,因为它使用ORM实体而不是域实体 Even using DOmain entities, it usable only as Domain Repositories, because you need a different interface definition for querying purposes. 即使使用DOmain实体,它也只能用作域存储库,因为您需要不同的接口定义来进行查询。 DEfine each repository interface as you need it. 根据需要定义每个存储库接口。

About pagination, just pass 'skip' and 'take' arguments to the repo method.It will work even if you don;t use an ORM, the repo will implement the pagination according to the DAO it uses to work with the db. 关于分页,只需将'skip'和'take'参数传递给repo方法。即使你不使用ORM也会工作; repo会根据它用来处理db的DAO实现分页。 And for your own good, try employing the CQRS mindset, it will make your DDD life much easier. 为了您自己的利益,尝试使用CQRS思维模式,它将使您的DDD生活更加轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM