是否应该将 IOrderedEnumerable
用作纯粹用于语义值的返回类型?
例如,在表示层消费模型时,我们如何知道集合是需要排序还是已经排序?
如果存储库使用 ORDER BY
子句包装存储过程,情况会怎样。存储库应该返回 IOrderedEnumerable
吗?如何实现?
最佳答案
我认为这不是个好主意:
Should IOrderedEnumerable be used as a return type purely for semantic value?
For example, when consuming a model in the presentation layer, how can we know whether the collection requires ordering or is already ordered?
如果您不知道序列是按哪个键排序的,那么知道它是有序的有什么意义呢? IOrderedEnumerable
接口(interface)的要点是能够添加次要排序标准,如果您不知道什么是主要标准,这就没有多大意义。
What about in the case that a repository wraps a stored procedure with an ORDER BY clause. Should the repository return IOrderedEnumerable? And how would that be achieved?
这没有意义。正如我已经说过的,IOrderedEnumerable
用于添加辅助排序条件,但是当存储过程返回数据时,它已经排序并且添加辅助排序条件为时已晚。您所能做的就是完全重新排序,因此对结果调用 ThenBy
不会产生预期的效果。
关于c# - 什么时候返回 IOrderedEnumerable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8518557/