模式检索复杂的对象图与实体框架Repository模式 [英] Pattern for retrieving complex object graphs with Repository Pattern with Entity Framework

查看:118
本文介绍了模式检索复杂的对象图与实体框架Repository模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用实体框架抽象与信息库和模式的UnitOfWork一个ASP.NET MVC的网站。我不知道是其他人如何实现这些图案复杂的对象图导航。让我给我们的控制器中的一个例子:

We have an ASP.NET MVC site that uses Entity Framework abstractions with Repository and UnitOfWork patterns. What I'm wondering is how others have implemented navigation of complex object graphs with these patterns. Let me give an example from one of our controllers:

var model = new EligibilityViewModel
   {
       Country = person.Pathway.Country.Name,
       Pathway = person.Pathway.Name,
       Answers = person.Answers.ToList(),
       ScoreResult = new ScoreResult(person.Score.Value),
       DpaText = person.Pathway.Country.Legal.DPA.Description,
       DpaQuestions = person.Pathway.Country.Legal.DPA.Questions,
       Terms = person.Pathway.Country.Legal.Terms,
       HowHearAboutUsOptions = person.Pathway.Referrers
   };

这是一个注册过程和pretty所有事情挂出POCO类Person。在这种情况下,我们缓存通过注册程序的人。现在我已经开始实施注册过程需要访问数据对象图中更深层次的后半部分。具体来说DPA数据挂接在法律里面国家。

It's a registration process and pretty much everything hangs off the POCO class Person. In this case we're caching the person through the registration process. I've now started implementing the latter part of the registration process which requires access to data deeper in the object graph. Specifically DPA data which hangs off Legal inside Country.

上面的code只是映射出模型信息转化为视图模型一个简单的格式。我的问题是你认为这个图形良好做法相当深的导航或将对象你抽象出的检索进一步下跌的图形库成?

The code above is just mapping out the model information into a simpler format for the ViewModel. My question is do you consider this fairly deep navigation of the graph good practice or would you abstract out the retrieval of the objects further down the graph into repositories?

推荐答案

在我看来,这里最重要的问题是 - ?有你禁用惰性加载

In my opinion, the important question here is - have you disabled LazyLoading?

如果你什么也没做,那么它在默认情况下的上。

If you haven't done anything, then it's on by default.

所以,当你 Person.Pathway.Country ,你将被调用另一个调用数据库服务器(除非你做预先加载,这我会说话大约在某一时刻)。鉴于你使用Repository模式 - 这是一个很大的禁忌。控制器应该不会造成数据库服务器直接调用。

So when you do Person.Pathway.Country, you will be invoking another call to the database server (unless you're doing eager loading, which i'll speak about in a moment). Given you're using the Repository pattern - this is a big no-no. Controllers should not cause direct calls to the database server.

在一个 C ontroller已收到从 M 奥德尔的信息,它应该是准备做预测(如有必要),并传递到 V IEW,不走的的的 M 奥德尔。

Once a C ontroller has received the information from the M odel, it should be ready to do projection (if necessary), and pass onto the V iew, not go back to the M odel.

这就是为什么在我们的实现(我们还可以使用存储库,EF4和工作单位),我们的禁用延迟加载,然后允许通过通过我们的服务层的导航性能的通行证(一系列包括语句,通过枚举和扩展方法)发甜。

This is why in our implementation (we also use repository, ef4, and unit of work), we disable Lazy Loading, and allow the pass through of the navigational properties via our service layer (a series of "Include" statements, made sweeter by enumerations and extension methods).

我们再渴望负荷这些属性的控制器需要他们。但重要的是,的控制器必须明确地提出要求。

We then eager-load these properties as the Controllers require them. But the important thing is, the Controller must explicitly request them.

这基本上告诉UI - 嘿,你只收到相关实体的核心内容。如果您想别的,自讨苦吃。

Which basically tells the UI - "Hey, you're only getting the core information about this entity. If you want anything else, ask for it".

我们也有一个服务层的控制器和存储库之间进行调解(我们的仓库返回的IQueryable< T> )。这使得库摆脱处理复杂的关联业务。在预先加载在服务层(以及类似的东西分页)来完成。

We also have a Service Layer mediating between the controllers and the repository (our repositories return IQueryable<T>). This allows the repository to get out of the business of handling complex associations. The eager loading is done at the service layer (as well as things like paging).

服务层的好处是简单 - 更松散耦合。资源库仅处理添加,删除,查找(返回IQueryable的),工作单位处理特区的newing,而变化的Commiting,服务层处理实体物化为具体的集合。

The benefit of the service layer is simple - more loose coupling. The Repository handles only Add, Remove, Find (which returns IQueryable), Unit of Work handles "newing" of DC's, and Commiting of changes, Service layer handles materialization of entities into concrete collections.

这是一个不错的,1-1堆栈式的方法:

It's a nice, 1-1 stack-like approach:

personService.FindSingle(1, "Addresses") // Controller calls service
 |
 --- Person FindSingle(int id, string[] includes) // Service Interface
      |
       --- return personRepository.Find().WithIncludes(includes).WithId(id); // Service calls Repository, adds on "filter" extension methods
           |
            --- IQueryable<T> Find() // Repository
                |
                 -- return db.Persons; // return's IQueryable of Persons (deferred exec)

我们还没有起身到MVC层,但(我们正在做TDD),而是一个服务层可以是其他地方你可以的水合物的核心实体到的ViewModels。并再次 - 这将是到控制器决定它多少信息希望

We haven't got up to the MVC layer yet (we're doing TDD), but a service layer could be another place you could hydrate the core entities into ViewModels. And again - it would be up to the controller to decide how much information it wishes.

再次,这是所有关于松耦合。您的控制器应该尽可能简单化,而不是担心复杂的关联。

Again, it's all about loose coupling. Your controllers should be as simplistic as possible, and not have to worry about complex associations.

多少库,这是一个高度争议的话题方面。有些人喜欢有每个实体(矫枉过正,如果你问我),有的像基于功能组(有道理在功能方面,更易于使用)之一,然而我们有每个聚合根之一。

In terms of how many Repositories, this is a highly debated topic. Some like to have one per entity (overkill if you ask me), some like to group based on functionality (makes sense in terms of functionality, easier to work with), however we have one per aggregate root.

我只能猜测你的模型,人应该是唯一的聚合根,我可以看到。

I can only guess on your Model that "Person" should be the only aggregate root i can see.

因此​​,它不会使有另一个仓库办理路,当一个途径总是与特定的人相关多大意义。该人士版本库应该处理这个问题。

Therefore, it doesn't make much sense having another repository to handle "Pathways", when a pathway is always associated with a particular "Person". The Person repository should handle this.

再次 - 也许,如果你screencapped你EDMX,我们可以给你更多的提示

Again - maybe if you screencapped your EDMX, we could give you more tips.

这个答案可能会延伸出来的有点过头基础上,问题的范围,但认为我给了深入的答案,因为我们现在处理这个确切的情况。

This answer might be extending out a little too far based on the scope of the question, but thought i'd give an in-depth answer, as we are dealing with this exact scenario right now.

心连心。

这篇关于模式检索复杂的对象图与实体框架Repository模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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