在没有存储库的实体框架中的可重用查询。怎么样? [英] Reusable Querying in Entity Framework WITHOUT Repository. How?

查看:66
本文介绍了在没有存储库的实体框架中的可重用查询。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说,我得出的结论是(经过大量试验)存储库&使用Entity Framework时的工作单元是错误,错误,错误和这说明了原因很好。

Let me say, I have come to the conclusion (after a lot of trial) that Repository & Unit of Work when using Entity Framework is just wrong, wrong, wrong and this says why quite well.

但是我真的很讨厌那些嵌入式查询。问题是,如果我反对使用存储库等,该将它们放在哪里? (请提供简洁的答案,示例值得赞赏)。

But I really hate on those embedded queries. Question is, where can I put them instead if I'm so against a repository, etc? (clean answers only please, examples much appreciated).

我刚刚取消了两个项目,其中包含我的存储库,工作单元以及与数百个接口文件,因为回报无处可寻。我认为很多人,包括我自己在内,都跳上了Repository潮流,因为那是其他所有人都在做的事,但回想起来,我认为那真的是无处可去。

I just nuked two projects containing my repositories, unit of work and interfaces with hundreds of files because the payback was nowhere to be seen. I think lots of people, myself included, just jumped on the Repository bandwagon because that's what everybody else was doing but in retrospect, I think it's really a ride to nowhere.

/ sigh

理查德

推荐答案

您希望将它们放在哪里?您只有几个选择:

Where do you expect to put them? You have only few choices:


  1. 让它们成为它们的位置并使用自定义扩展方法,查询视图,映射的数据库视图或自定义定义查询以定义可重复使用的部分

  2. 将每个查询作为方法暴露在一些单独的类上。该方法不得公开 IQueryable ,并且不得接受 Expression 作为参数=整个查询逻辑必须包装在方法。但是,这将使您的类涵盖与存储库(唯一可以被嘲笑或伪造的存储库)类似的相关方法。此实现与用于存储过程的实现非常接近。

  3. 您将执行与先前方法相同的操作,但不是将查询放在单独的类中,而是将它们作为静态方法直接放置到实体中。由于静态方法不能被模拟替代(它需要更复杂的测试框架),因此可测试性更差。这是活动记录模式的一部分,其中每个实体负责将其加载并保存到数据库。 / li>
  1. Let them be where they are and use custom extension methods, query views, mapped database views or custom defining queries to define reusable parts
  2. Expose every single query as method on some separate class. The method mustn't expose IQueryable and mustn't accept Expression as parameter = whole query logic must be wrapped in the method. But this will make your class covering related methods much like repository (the only one which can be mocked or faked). This implementation is close to implementation used with stored procedures.
  3. You will do the same as in previous method but instead of placing queries in separate class you will put them as static methods to entity directly. This is much worse testable because static methods cannot be replaced by mocking (it requires more complex testing framework). This is part of active record pattern where each entity is responsible for its loading and saving to database.

自定义扩展方法示例:

public static IQueryable<TEntity> GetByName(this IQueryalbe<TEntity> query, string name) 
    where TEntity : IEntityWithName
{
    return query.Where(e => e.Name == name);
}

自定义类暴露方法示例:

Example of custom class exposing methods:

public class QueryProvider
{
    public QueryProvider() {}

    public IEnumerable<TEntity> GetByName(IYourContext context, string name)
        where TEntity : IEntityWithName
    {
        return context.CreateObjectSet<TEntity>().Where(e => e.Name == name).ToList();
    }
}

这篇关于在没有存储库的实体框架中的可重用查询。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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