我们是否应该使用Data Repository模式使用实体框架code第一种方法的MVC应用程序? [英] Should we use Data Repository pattern in MVC application using Entity Framework Code First approach?

查看:109
本文介绍了我们是否应该使用Data Repository模式使用实体框架code第一种方法的MVC应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经开发了许多应用程序与实体框架code第一种方法。在所有的我使用Data Repository模式。这个数据仓库模式在查询时间超过单个实体。对于例如,

I have developed many application now with Entity Framework Code First approach. In all of the I use Data Repository pattern. This Data Repository pattern queries over single entity at a Time. for e.g,

我有2个型号
员工和部门

I have 2 models Employee and Department

所以下载所有的员工和部门我将创建2数据存储库实例。例如

So to fetch all employees and department I would create 2 data repository instances. e.g

var empRepository = new DataRepository<Employee>();
var allEmployees = empRepository.GetAll();

var depRepository = new DataRepository<Department>();
var alldepartment = depRepository.GetAll();

现在,这种模式在大多数的情况下的伟大工程。现在,当我想执行加入我不能这样做,在这种模式。我可以执行只能加入后,我已经赚得了两个实体的所有记录,然后我就可以使用加入在内存中的数据。这会在我的逻辑2的查询额外的开销。没有任何一个有可与DataRepository模式中使用的良好格局或解决方案。请提出任何替代这种模式。

Now, This pattern works great in most of the case. Now, When I want to perform join I can not do that in this pattern. I can perform join only after I have fetched all records of both entities and then i can use join on in-memory data. this creates extra overhead of 2 queries in my logic. Does any one have good pattern or solution that can be used with DataRepository pattern. Please suggest any alternatives to this pattern.

推荐答案

我其实只是昨天刚实施了我的通用存储库中加入的功能。这是一个容易得多比任何人是如何制作出来的人做的,你可以使用实体框架的一些很酷的功能,而这样做。

I actually just implemented a Join function in my generic repository just yesterday. It's a lot easier to do than how everyone's making it out to be, and you can use some cool features of Entity Framework while doing so.

要开始,我使用一个存储库类似于我的这个博客帖子。你会注意到,一些方法正在返回的IQueryable 。这绝非偶然。 的IQueryable 将允许仍然使用延迟执行,这意味着该查询不会在数据库中,直到一些它强制(即环或 .ToList()调用)。你会看到,随着把加入这种类型的存储库,那你会不会最终需要的所有实体加载到内存中做加盟,因为所有你会暴露是的IQueryable

To get started, I'm using a repository similar to what I wrote in this blog post. You'll notice that a number of the methods are returning IQueryable. This is no accident. IQueryable will allow to still use deferred execution, meaning that the query won't be run on the database until something forces it to (i.e. a loop or a .ToList() call). You'll see that, with putting the Join on this type of repository, that you won't end up needing to load up all the entities into memory to do the join, since all you'd be exposing is an IQueryable.

话虽这么说,这里是如何我有我的存储库中的加入,基于关闭的的加入方式:

That being said, here's how I've got the Join in my repository, based off of the Queryable version of the Join method:

public IQueryable<TResult> Join<TInner, TKey, TResult>(IRepository<TInner> innerRepository, Expression<Func<T, TKey>> outerSelector, Expression<Func<TInner, TKey>> innerSelector, Expression<Func<T, TInner, TResult>> resultSelector) where TInner : class
{
  return DbSet.Join(innerRepository.All(), outerSelector, innerSelector, resultSelector);
}

那么,这不仅使一个查询数据库来获取所有的,你要求(同样的信息,因为它只在传递一个的IQueryable 所有()法)。

这篇关于我们是否应该使用Data Repository模式使用实体框架code第一种方法的MVC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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