多个或单个存储库与LINQ [英] Multiple or Single Repositories with LINQ

查看:161
本文介绍了多个或单个存储库与LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读的专业ASP.NET MVC 1.0本书第11章(可测试设计模式)。在这一章中的例子,数据访问被分成若干存储库:IOrderRepository,IProductRepository,等等这一切都有道理。一个单类数据的单个存储库。

I've been reading Chapter 11 (Testable Design Patterns) in the Professional ASP.NET MVC 1.0 book. In the examples in this chapter, data access is split into a number of repositories: IOrderRepository, IProductRepository, etc. That all makes sense: a single repository for a single class of data.

不过,这个有点坏了,我当你考虑到表之间的联系:订单中包含了许多产品。当通过LINQ到SQL创建Order类,订单类将有一个产品集合,枚举所有与该订购产品。因此,通过使用此集合我们绕过ProductsRepository。

However, this breaks down somewhat for me when you consider the links between the tables: an Order contains a number of Products. When the Order class is created by LINQ-to-SQL, the order class will have a Products collection that enumerates all of the products related to that order. So by using this collection we're bypassing the ProductsRepository.

因此​​嘲讽的时候,我将不仅要嘲笑OrderRepository和ProductRepository,我也不得不填充产品集合在返回Order对象。这意味着嘲笑OrderRepository有了解的嘲笑ProductsRepository等。

Therefore when mocking, I'm going to not only have to mock the OrderRepository and the ProductRepository, I'm also going to have to populate the Products collection in the returned Order objects. Which means that the mocked OrderRepository has to know about the mocked ProductsRepository and so on.

这似乎是一种浪费忽略LINQ这样好心为我创造了这些藏品,所以我的问题是,在这种情况下不会单个存储库更有意义?

It seems a waste to ignore these collections that LINQ so kindly created for me, so my question is, in this case wouldn't a single repository make more sense?

推荐答案

其他需要考虑的因素包括产品的可能寿命,具有任何从LINQ到SQL来改变一些其他的O / R映射器的可能性点中的寿命。较小的项目,不太重要的产品,你需要的少操心抽象minutae到第n程度。

Other factors to consider include the likely lifespan of the product and the likelihood of having to change from LINQ-to-SQL to some other O/R mapper at any point in the lifetime. The smaller the project, the less critical the product, the less you need to worry about abstracting minutae to the nth degree.

这篇关于多个或单个存储库与LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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