gpt4 book ai didi

linq-to-sql - 如何映射 LINQ To SQL 以启用预加载,返回 EntitySet 或 ICollection?

转载 作者:行者123 更新时间:2023-12-02 03:55:51 24 4
gpt4 key购买 nike

这与我的问题相关(但相当独立):Why SELECT N + 1 with no foreign keys and LINQ?

我试过使用 DataLoadOptions 来强制预先加载,但我没有让它工作。

我正在手动编写我的 LinqToSQL 映射,并且首先遵循本教程:http://www.codeproject.com/Articles/43025/A-LINQ-Tutorial-Mapping-Tables-to-Objects

现在我找到了这个教程:http://msdn.microsoft.com/en-us/library/bb386950.aspx

我至少可以发现一个主要区别。第一个教程建议返回 ICollection,第二个教程建议返回 EntitySet。由于遇到问题,我尝试切换我的代码以返回 EntitySet,但后来我遇到了需要在我的 View 和 Controller 中引用 System.Data.Linq 的问题。我试图这样做,但没有成功。我也不确定这是个好主意。

在这一点上,我只想知道我应该使用哪种返回类型来进行良好的设计?我是否可以拥有良好的设计并且仍然能够在特定情况下强制预加载?

最佳答案

大量的试验和错误最终导致了解决方案。可以回ICollectionIList ,或者在某些情况下 IEnumerable .有些人认为返回 EntitySetIQueryable是个坏主意,我同意,因为它暴露了很多数据源/技术。有些东西回来了 IEnumerable是个坏主意,它似乎取决于。问题在于它可以用于延迟加载,这可能是好事,也可能不是好事。

一个反复出现的问题是返回分页结果,其中包含页面外的总项目数。这可以通过创建 CollectionPage<T> 来解决。 ( http://www.codetunnel.com/blog/post/104/how-to-properly-return-a-paged-result-set-from-your-repository )

更多关于从存储库返回的内容:

http://www.codetunnel.com/blog/post/103/should-you-return-iqueryablet-from-your-repositories

http://www.shawnmclean.com/blog/2011/06/iqueryable-vs-ienumerable-in-the-repository-pattern/

IEnumerable vs IQueryable for Business Logic or DAL return Types

List, IList, IEnumerable, IQueryable, ICollection, which is most flexible return type?

更重要的是,DataLoadOptions 可以进行预先加载!我现在已经对我的代码进行了如此多的重组,我不是 100% 确定我做错了什么导致 DataLoadOptions不工作。据我所知,如果我试图将它添加到 DataContext 中,我应该会得到一个异常(exception)。在DataContext之后已被使用,它没有。我发现的是在工作单元模式中思考。但是,出于我的需要(并且因为我不想从我的存储库返回 EntitySetIQueryable),我不打算实现跨存储库的工作单元。相反,我只是将我的存储库方法视为它们自己的小工作单元。我敢肯定这有不好的地方(例如,在某些更新情况下它可能会导致更多的数据库往返),将来我可能会重新考虑。然而,这是一个简单干净的解决方案。

更多信息在这里:

https://stackoverflow.com/a/7941017/1312533

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

这是我最终在我的存储库中得到的:

public class SqlLocalizedCategoriesRepository : ILocalizedCategoriesRepository
{
private string connectionString;
private HttpContextBase httpContext;
public SqlLocalizedCategoriesRepository(string connectionString, HttpContextBase httpContext) // Injected with Inversion of Control
{
this.connectionString = connectionString;
this.httpContext = httpContext;
}

public CollectionPage<Product> GetProductsByLocalizedCategory(string category, int countryId, int page, int pageSize)
{
// Setup a DataContext
using (var context = new DataContext(connectionString)) // Because DataContext implements IDisposable it should be disposed of
{
var dlo = new System.Data.Linq.DataLoadOptions();
dlo.LoadWith<Product>(p => p.ProductSubs); // In this case I want all ProductSubs for the Products, so I eager load them with LoadWith. There's also AssociateWith which can filter what is eager loaded.
context.LoadOptions = dlo;
context.Log = (StringWriter)httpContext.Items["linqToSqlLog"]; // For logging queries, a must so you can see what LINQ to SQL generates

// Query the DataContext
var cat = (from lc in context.GetTable<LocalizedCategory>()
where lc.CountryID == countryId && lc.Name == category
select lc.Category).First(); // Gets the category into memory. Might be some way to not get it into memory by combining with the next query, but in my case my next step is that I'm also going to need the Category anyway so it's not worth doing because I'm going to restructure this code to take a categoryId parameter instead of the category parameter.

var products = (from p in context.GetTable<Product>()
where p.ProductCategories.Any(pm => pm.Category.CategoryID == cat.CategoryID)
select p); // Generates a single query to get the the relevant products, which with DataLoadOptions loads related ProductSubs. It's important that this is just a query and not loaded into memory since we're going to split it into pages.

// Return the results
var pageOfItems = new CollectionPage<Product>
{
Items = products.Skip(pageSize * (page - 1)).Take(pageSize).ToList(), // Gets the page of products into memory
TotalItems = products.Count(), // Get to total count of items belonging to the Category
CurrentPage = page
};
return pageOfItems;
}
}
}

关于linq-to-sql - 如何映射 LINQ To SQL 以启用预加载,返回 EntitySet 或 ICollection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12661398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com