Should sorting logic be placed in the model, the view, or the controller?

I have a drop down list that displays values from a table to the end user. I would like to have these values be sorted alphabetically.

According to proper MVC design, at what layer should I place my sorting logic: the model, the view, or the controller?

EDIT : In response to LarsH's question, "Do you mean code that determines what sort order is desired? or code that performs the sort?", I was originally referring to the code that determines what sort order is desired.


(Note: this quote and citation is taken from @dasblinkenlight's answer, but we disagree on our interpretation of it. read his post and make up your own mind).

According to MVC description,

A controller can send commands to its associated view to change the view's presentation of the model (for example, by scrolling through a document). It can send commands to the model to update the model's state (eg editing a document).

Sorting logic (eg, the sorting comparator/sorting algorithm) belongs in the model since it contains business rules and state data. Since altering the way the model data is sorted falls squarely into the "change the view's presentation of the model" category, the controller is responsible for "doing the sorting" by calling the model.changeSortedState() method.


Who controls the sort order?

(From Wikipedia)

1) Natural order within the data itself:

The order is part of the Model, so it should go there. A raw pull of "all data" would return the data in the sorted order, and there is no interface to choose the sort order.

2) The user should control how they see the data:

The View would provide an interface (such as ascending/descending arrows) that interact with the Controller, and the Model understands the data well enough to do the requested sort on the data. However, a raw pull of the data doesn't necessarily have to be sorted, unlike in (1).

In either case,

The View doesn't understand that there's a sort going on, other that the ability to show which sort direction has been chosen. Don't put the logic there.

Small caveat

The sorting functionality could go purely in the View, under one circumstance (that I can think of offhand; there may be more):

A "dumb" sort where all the data is already in the view and it doesn't have to use any domain knowledge to do the sort. Very simple string or number comparison, for example. This is not possible in, for example, search results on a webpage when results are likely to be split across multiple pages.


According to MVC description,

A controller can send commands to its associated view to change the view's presentation of the model (for example, by scrolling through a document). It can send commands to the model to update the model's state (eg editing a document).

According to this, sorting logic belongs in the controller, because altering the way the model data is sorted falls squarely into the "change the view's presentation of the model" category.

EDIT: To clarify multiple misunderstandings voiced in the comments, the "sorting logic" is not the code that performs the sort; it is the code that defines the sort. The sorting logic compares individual items to each other to establish an order (eg through an instance of IComparator<T> ) or contains logic that constructs an object to be used for ordering by an external system (eg through an instance of IOrderedQueryable<T> ). This logic belongs in your controller, because it needs knowledge related to the "business" side of your application. It is entirely sufficient to perform the sort, but it is separate from the code that actually performs it. The code that sorts may be in your view, in your model, or even in the persistence layer that backs your model (eg your SQL database).

链接地址: http://www.djcxy.com/p/21500.html

上一篇: scala邮箱大小限制

下一篇: 排序逻辑应放置在模型,视图还是控制器中?