IQueryable的&安培;库 - 取2? [英] IQueryable & Repositories - take 2?

查看:160
本文介绍了IQueryable的&安培;库 - 取2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不承认,我一直背着仓库不应该返回的IQueryable的旗帜,因为这是很难测试。我一直在回答其他问题如这和<一个href=\"http://stackoverflow.com/questions/718624/to-return-iqueryablet-or-not-return-iqueryablet\">this.

I have to admit I have been carrying the "Repository should not return IQueryable" banner because it is harder to test. I have been influenced by the answers to other questions like this and this.

今天上午我一直在阅读ScuttGu的关于<一个博客href=\"http://weblogs.asp.net/scottgu/archive/2011/09/05/web-forms-model-binding-part-1-selecting-data-asp-net-vnext-series.aspx\"相对=nofollow> ASP.NET vNext ,他描述了使用似乎依赖于IQueryable的分​​页和排序一个SelectMethod模型绑定。

This morning I've been reading ScuttGu's blog about ASP.NET vNext where he details Model Binding using a SelectMethod that seems to rely on IQueryable for paging and sorting.

你是否认为这将迫使我们重新考虑存储库的IQueryable扮演的角色?

Do you think this will force us to reconsider the role IQueryable plays in repositories?

推荐答案

DDD库应该封装数据访问的技术性问题:

DDD Repository should encapsulate data access technicalities:

定义:一个库是用于封装的存储机制,
  检索和搜索行为可模拟对象的集​​合。

Definition: A Repository is a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects.

也负责处理中,域对象的生命结束。存储库接口属于域,并应根据通用语言尽可能。除了DDD的书,这两篇文章几乎涵盖了你在设计时库需要知道的一切:

It is also responsible for handling middle and end of life of domain objects. Repository interface belongs to Domain and should be based on Ubiquitous Language as much as possible. In addition to DDD book, these two articles cover almost everything you need to know when designing repositories:

存储库接口IQueryable的暴露是不是在我看来,最佳的。 IQueryable的是不是通用语言的一部分。它是一个技术性,它没有域的含义。而不是封装数据检索库将暴露的裸数据访问机制,本质上违背了摆在首位有仓库的目的。

Exposing IQueryable on the Repository interface is not optimal in my opinion. IQueryable is not part of Ubiquitous Language. It is a technicality, it has no domain meaning. Instead of encapsulating data retrieval Repository would expose bare data access mechanism, which essentially defeats the purpose of having Repository in the first place.

关于ASP.NET。这是一个UI框架。为什么你会允许UI框架来影响你的域模型的设计?微软的例子很多时候鼓励之类的东西,直接绑定到你的数据库表中的UI数据网格。或者说,最近一段时间,控制绑定到什么被称为域模型,而它实际上是贫血型号,或者干脆哑数据容器获取/套。从文章中引用你提到(我把一些重点):

Regarding ASP.NET. This is a UI framework. Why would you allow UI framework to affect the design of you domain model? Microsoft examples often times encourage things like UI data grid binded directly to your database tables. Or, most recently, controls binded to what is referred to as Domain Model, while it is in fact Anemic Model, or simply dumb data container with gets/sets. The quote from the article that you mention (I put some emphasis):

示范绑定是code为中心的方法为数据绑定即可。它允许
  你写的 CRUD 帮手$ C $中的方法C-后面的文件
  页面,然后轻松它们连接到内的任何服务器控件
  页。然后,服务器控件将采取调用方法护理
  在页面生命周期的适当时间和数据绑定数据

Model binding is a code-focused approach to data-binding. It allows you to write CRUD helper methods within the code-behind file of your page, and then easily wire them up to any server-controls within the page. The server-controls will then take care of calling the methods at the appropriate time in the page-lifecycle and data-bind the data.

这个我间pretation是抛弃模型和对象,只是你的数据绑定到用户界面。这可能是在很多情况下的有效和合理的方式。但由于这个问题被标记为DDD我要说的是在DDD这就是所谓的<一个href=\"http://$c$cidol.com/csharp/domain-driven-design/Isolating-the-Domain/The-Smart-UI-Anti-Pattern/\">Smart UI反模式。

My interpretation of this is to ditch the model and objects and just bind your data to UI. This is probably a valid and justified approach in a lot of cases. But since the question was tagged as DDD I would say that in DDD this is called Smart UI Anti-Pattern.

这篇关于IQueryable的&安培;库 - 取2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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