应该在模型,视图或控制器中放置排序逻辑吗? [英] Should sorting logic be placed in the model, the view, or the controller?

查看:88
本文介绍了应该在模型,视图或控制器中放置排序逻辑吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,用于显示从表到最终用户的值.我想按字母顺序对这些值进行排序.

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.

根据正确的MVC设计,我应该在哪一层放置排序逻辑:模型,视图还是控制器?

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

编辑:在回答LarsH的问题时,您是指确定所需排序顺序的代码还是执行排序的代码?",我最初指的是确定什么内容的代码.需要排序顺序.

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.

推荐答案

(注意:此引用和引文摘自 @dasblinkenlight的答案,但我们不同意对此的理解.请阅读他的文章,然后下定决心).

(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).

根据 MVC描述

控制器可以将命令发送到其关联的视图,以更改模型的视图表示(例如,通过滚动文档).它可以向模型发送命令以更新模型状态(例如,编辑文档).

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 (e.g. editing a document).

排序逻辑(例如,排序比较器/排序算法)属于模型,因为它包含业务规则和状态数据.由于改变模型数据的排序方式直接属于更改视图的模型显示"类别,因此控制器通过调用model.changeSortedState()方法负责进行排序".

Sorting logic (e.g., 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.

这篇关于应该在模型,视图或控制器中放置排序逻辑吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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