开发者

linq2sql, repository pattern - how to query data from two or more tables?

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

In v开发者_JAVA百科ery 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.

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.

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?

Thanks.


Think about the statement you made:

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.

That smells trouble. Create a repository to facilitate behaviour in a isolated area of your domain, not for each business entity.

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.

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.

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

Then to do your above query:

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.

HTH.


var customers = from cust in CustomersRep
                join prod in ProductsRep on prod.CustomerID equals cust.CustomerID
                where prod.ProductName == yourInput
                select cust;

You can write a query, similar to you would in SQL (essentially). In the above yourInput is the variable that holds the product name you're interested in.

This should give you a single DB query request, so long as ProductsRep and CustomersRep are in the same DB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜