LINQ2SQL,存储库模式 - 如何从两个或多个表中查询数据? [英] linq2sql, repository pattern - how to query data from two or more tables?

查看:140
本文介绍了LINQ2SQL,存储库模式 - 如何从两个或多个表中查询数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Repository模式(和LINQ2SQL作为数据存取),并具有例如,ProductsRep和CustomersRep。

I use Repository pattern (and linq2sql as data access) and have, for example, ProductsRep and CustomersRep.

在很简单的场景数据库有两个表 - Produsts 的(产品ID,客户ID,产品名称,日期)和客户的(客户ID,名字,姓氏)。

In very simple scenario db has two tables - Produsts (ProductID, CustomerID, ProductName, Date) and Customer (CustomerID, FirstName, LastName).

每个库提供的方法来创建,更新,删除和获取具体型号的对象,可能是,一些过滤器。

Each repository provide methods to create, update, delete and get specific model object, and, may be, some filters.

但是,如果我想查询由产品名购买特定产品的所有客户,我得使用ProductsRep本产品的产品ID,然后得到购买产品的使用CustomersRep此ID的所有客户。

But if I want to query all customers that buy specific product by product name, I have to get ProductID of this product using ProductsRep and then get all customers that buy product with this id using CustomersRep.

我说得对不对?这是L2S必须产生实际上是两个SQL请求,是有可能这样做只有一个要求吗?

Am I right? This is actually two sql requests that l2s must generate, is it possible to do only one request?

和,一般来说,如果我们要使用多个表与关系和存储库的模式来查询数据时,如何做到这一点通过查询量减少到最低限度?

And, in general, if we want to query data using multiple tables with relationships and repository pattern, how to do this by reduce amount of queries to minimum?

感谢。

推荐答案

想想你的发言:

但是,如果我想查询所有客户
  根据产品购买特定产品
  名字,我得这个产品ID
  使用产品ProductsRep然后得到
  那买的产品与所有客户
  这个ID使用CustomersRep。

But if I want to query all customers that buy specific product by product name, I have to get ProductID of this product using ProductsRep and then get all customers that buy product with this id using CustomersRep.

这闻起来麻烦。 创建信息库,以方便行为域的隔离区,而不是针对每个业务实体。

与DDD /存储库一个常见的​​错误是认为有关数据。别。想想行为。

A common mistake with DDD/Repository is to think about data. DONT. Think about behaviour.

您拥有的产品实体,指的客户即可。这意味着产品离不开客户的存在。对?因此,一个客户是聚合根 - 它控制你的域模型的这一部分的行为

You have a Products entity which refers to a Customer. Which means a Product cannot exist without a customer. Right? Therefore a Customer is your aggregate root - it controls the behaviour of this portion of your domain model.

您上面的回答评论确认 - 要跨多个存储库工作。你有两个选择 - 1)的工作单位,2)修复了你的资料库的设计

Your comment to the answer above confirms that - you are working across multiple repositories. You have two options - 1) unit of work, 2) fix up your repository design.

您只需要1库 - 我们称之为 CustomerProductRepository

You only need 1 Repository - let's call it CustomerProductRepository.

然后做你上面的查询:

var customersWhoBuyHats = customerProductRepository
                             .Products
                             .Include("Customer")
                             .Where(x => x.ProductName == "Hat")
                             .Select(x => x.Customer)
                             .ToList();

结果是一个 列表<客户方式> ,然后你与一个数据库调用做到了

The result is a List<Customer>, and you did it with one database call.

心连心。

这篇关于LINQ2SQL,存储库模式 - 如何从两个或多个表中查询数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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