优化分页&使用具有 EnableCaching = true 的 ObjectDataSource 进行排序 [英] Optimize Pagination & Sorting with ObjectDataSource having EnableCaching = true

查看:14
本文介绍了优化分页&使用具有 EnableCaching = true 的 ObjectDataSource 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Linq-To-SQL 类的 ODS(ObjectDataSource) 来填充页面上的 Gridview.

I'm using an ODS(ObjectDataSource) backed-up with a Linq-To-SQL class to populate Gridview on my page.

考虑到性能 - 我禁用了 Gridview 的 Viewstate 并启用了 ODS 中的缓存.

Considering the performance - I've disabled the Viewstate of the Gridview and enabled caching in the ODS.

除此之外,我还优化了 Linq-to-SQL 类中的 Search 方法以使用 .skip &.take 方法只获取一页"的记录.

Apart from this, I've also optimized the Search method in the Linq-to-SQL class to use the .skip & .take methods to fetch only a 'pageful' of records.

现在,问题是由于缓存,ODS 无法自行对记录集进行排序".如该线程所述:

Now, the problem is that due to caching the ODS is unable to 'sort' the record set on its own. As detailed on this thread:

在自定义分页和排序中启用缓存时,GridView 排序不起作用

人们建议使用自定义排序并实现比较器",但我相信这会破坏我的性能优势.

People recommend to use custom sort and implement 'Comparer' but I believe this ruins my performance benefits.

http://forums.asp.net/t/1344883.aspx

我已准备好在排序时进行一次 DB-trip,但是如何在打开缓存时将其分开?

I'm ready to make a DB-trip while sorting but how to separate that when the Caching is turned-on?

仅供参考,我已经有一个 AJAX 更新面板,其中有这个 Gridview (EnableViewstate = false) 和 ODS (EnableCaching=true).希望我走在正确的道路上......感谢建议.

FYI, I already have an AJAX update panel in which I've got this Gridview (EnableViewstate = false) and ODS (EnableCaching=true). Hope I'm on the right path ... suggestions are appreciated.

我必须使用自定义排序"在应用程序端执行排序(即添加额外的方法以启用对通用对象集合的排序).这个解决方案是不可接受的,因为它要求我从数据库中提取所有记录,然后对它们进行排序!

I've to perform sorting on app side using the 'Custom-sort' (that is add extra methods to enable sorting on a generic collection of objects). This solution was not acceptable because it demanded that I pull ALL the records from the DB and then perform sorting on them!

首先,我不相信应用程序可以比 DB 进行更好/更快的排序.其次 - 这破坏了我因优化分页而获得的整体性能优势 - 我使用 .skip() 和 .take() LINQ 方法来仅获取整页"记录.

Firstly, I don't believe app can do better/faster sorting then DB. And secondly - this ruins the whole performance benefit that I'm getting due to optimized pagination - I'm using .skip() and .take() LINQ methods to fetch only a 'pageful' of records.

嗯,最后我不得不发明一个修复我自己的.它可能仅限于我的种类的场景,但肯定它很多更容易,也保留了两个分页的优化为以及数据缓存.

Well, finally I had to invent a fix of my own. It might be limited to my kind of scenario but for sure its much easier and also preserves the optimization of both pagination as well as data-caching.

我的解决方案:我点击了 Gridview 的排序"事件.如果我允许 ODS 尝试自行对缓存数据进行排序,则会触发自定义排序"错误.取而代之的是,现在我手动执行排序并取消排序事件.为此 - 我只需要在 ODS 中显式设置 'orderBy' 参数,并在 Gridview 的 'Sorting' 事件中将其设置为新的排序表达式.这将刷新网格,最后我会这样做:

MY SOLUTION: I've tapped the Gridview's 'Sorting' event. The 'custom-sort' error triggers if I allow the ODS to try to do the sorting on its own on the cached data. Instead of that, now I perform sorting manually and cancel the sorting-event. For this - I just have to make the 'orderBy' parameter explicit in ODS and set it to the new sort-expression in Gridview's 'Sorting' event. This will refresh the Grid and at the end I do:

odsOrganization.SelectParameters["orderBy"].DefaultValue = GetSortExpr(e.SortExpression);
...
e.Cancel = true;

  • 这告诉 ODS 取消排序(我之前已经执行过取消活动 - 如上所述).它的就像我在欺骗 ODS 和处理后台排序.谢谢到 ODS,它感觉到'SelectParameters["orderBy"]' 有更改并执行一次选择再次.I 前一种自动存储在'odsOrganization.SelectParameters["orderBy"].DefaultValue'我可以在后续使用迭代.

    this tells the ODS to cancel sorting (which I already performed before canceling the event - as above). ITs like I'm cheating the ODS and handling the sorting in background. And thanks to ODS that it senses that the 'SelectParameters["orderBy"]' has changed and performs a select once again. I The previous sort automatically gets stored in the 'odsOrganization.SelectParameters["orderBy"].DefaultValue' which I can use in sub-sequent iterations.

  • 我仍在测试这个,但令人惊讶的是,仅通过更新参数的 DefaultValue ODS 就可以返回获取数据.这会保留缓存,直到用户执行排序(它从缓存中获取数据用于其他操作)并返回到数据库进行排序.希望它对我有用!

    I'm still testing this one but its amazing that just by updating the parameter's DefaultValue ODS goes back to fetch the data. This preserves the cache until the user performs sort (it takes data from cache for other operations) and goes back to the DB to sort. Hope its going to work for me!

    推荐答案

    这行得通吗?

    http://dotnetarchitect.wordpress.com/2009/04/07/gridview-sorting-trick-when-using-object-datasource-with-custom-objects/

    这篇关于优化分页&使用具有 EnableCaching = true 的 ObjectDataSource 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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