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

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

问题描述

我使用 Repository 模式(和 linq2sql 作为数据访问)并且有,例如,ProductsRep 和 CustomersRep.

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

在非常简单的场景中,db 有两个表 - Produsts(ProductID、CustomerID、ProductName、Date)和 Customer(CustomerID、FirstName、LastName).

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 获取该产品的 ProductID,然后使用 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?

谢谢.

推荐答案

想想你的陈述:

但是如果我想查询所有客户按产品购买特定产品名称,我必须得到这个的 ProductID产品使用 ProductsRep 然后得到所有购买产品的客户此 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.

这闻起来很麻烦.创建一个存储库,以促进您域中孤立区域的行为,而不是针对每个业务实体.

DDD/Repository 的一个常见错误是考虑数据.别.考虑行为.

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

您有一个 Products 实体,它指的是 Customer.这意味着没有客户就不能存在产品.对?因此,客户是您的聚合根 - 它控制着域模型这部分的行为.

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();

结果是一个 List,您只需调用一次数据库即可.

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

HTH.

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

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