最佳实践 - 三层架构,排序和分页 [英] Best Practices - three tier arhitecture, sort and pagination

查看:99
本文介绍了最佳实践 - 三层架构,排序和分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络应用程序中有三层架构(asp.net)



应该在表示层或业务逻辑中进行排序和分页吗?



i have three tier arhitecture in web app (asp.net)

should sort and pagination be in presentation layer or business logic?

//Search.aspx.cs
public void SearchProjects(int recordCount)
{
    var data = DAL.GetData();

    var filtered = data.OrderBy(x => x.Key).Take(recordCount);

    //todo
}






or

//Search.aspx.cs
public void SearchProjects(int recordCount, object sortType)
{
    var data = DAL.GetData(recordCount, sortType);

    //todo
}

推荐答案

它取决于。 GetData返回什么?如果它返回IQuereable或IEnumerable,使得查询到达表示层时尚未出现问题,那么您可以在表示层执行paging\sorting。



如果GetData实际检索所有数据并将其作为内存数据存储区(如DataSet)返回,则列表< t>那么你不应该在表示层中使用page \ sort。



回到第一个场景......如果GetData返回IQuereable那么你可以认为那个不好设计,因为您的表示层与数据层紧密耦合,因为您的表示代码只能用于通过IQuereable支持延迟加载的数据存储。这意味着您的表示层通常需要知道数据存储才能完成其工作。如果GetData将所有行读入内存数据存储并返回,那么您就没有这些问题,但是您需要在数据层完成paging\sorting。
"It depends". What does GetData return? If it returns an IQuereable or IEnumerable such that the query hasn't yet been issues by the time it gets to the presentation layer then you could do the paging\sorting at the presentation layer.

If GetData actually retrieves all data and returns it as an in-memory datastore like DataSet, List<t> etc then you shouldn't page\sort in the presentation layer.

Getting back to the first scenario...if GetData returns IQuereable then you could consider that bad design as you are tightly-coupling your presentation layer to the data layer as your presentation code can only work with datastores that support lazy-loading via IQuereable. It means that your presentation layer will often need knowledge of the data store to do its work. If GetData reads all the rows into an in-memory data store and returns that then you don't have those issues, however your paging\sorting will need to be done at the data layer.


这篇关于最佳实践 - 三层架构,排序和分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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