Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to return 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?

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?

like image 937
fearofawhackplanet Avatar asked Dec 15 '11 10:12

fearofawhackplanet


1 Answers

I don't think it would be a good idea:

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?

What is the point in knowing that a sequence is ordered if you don't know by which key it is ordered? The point of the IOrderedEnumerable interface is to be able to add a secondary sort criteria, which doesn't make much sense if you don't know what is the primary criteria.

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?

This doesn't make sense. As I already said, IOrderedEnumerable is used to add a secondary sort criteria, but when the data is returned by the stored procedure, it is already sorted and it's too late to add a secondary sort criteria. All you can do is re-sort it completely, so calling ThenBy on the result wouldn't have the expected effect.

like image 197
Thomas Levesque Avatar answered Oct 19 '22 06:10

Thomas Levesque