决定 def 应该属于控制器还是模型 (Ruby)
所以这是一个相当新手的问题。我仍在编写网络蜘蛛,并且有很多问题,但我想问的第一个问题是如何决定一个方法应该属于控制器还是模型。
我不想将我的应用程序带入其中,因为有许多特定的“此代码是否属于控制器或模型”问题,而我希望这个问题仅作为一般准则。
So its a fairly noobish question. Im still coding a web spider and have lots of questions but the first one I want to ask is how do you decided whether a method should belong to a controller or a model.
I don't want to bring my application into this as there are many specific "does this code belong in controller or model" questions whereas I'm hoping this question will just serve as a general guideline.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我总是尽可能选择瘦控制器、胖模型 - 所以你的问题的答案通常是模型。
I always go with Skinny Controller, Fat Models as far as I can - so the answer to your question is usually the model.
我曾在多种环境、多种语言中工作过,从完全过程式到面向对象但不是 MVC,再到具有胖控制器的 MVC 和具有瘦控制器的 MVC。我只能说出我自己的观点,但这些都是我多年来学到的东西,也是我通过经验以及必须处理我编写的一些早期代码的维护后果而获得的观点(我们都有过去的经历) !)。
我也知道很多人会不同意我在这里写的内容,因为这就是我们工作的本质;)
我在这里偏离了主题,但简而言之,您的大部分逻辑可能应该在您的模型中完成。您的模型根据数据以及数据的变化方式对您的应用程序进行建模。因此,很自然地,这是放置大量逻辑的地方。您的控制器仅用于在模型和最终用户之间传输信息(这正是通过视图发生的)。
说“我同意约翰的观点”是相当冗长的方式,不是吗? ;)
I've worked in many environments, in many languages, ranging from entirely procedural, to object-oriented but not MVC, to MVC with fat controllers and MVC with skinny controllers. I can only speak of my own opinions, but these are things I've learned over the years and opinions I've gained through experience and having to deal with the maintenance consequences of some of the early code I wrote (we all have a past!).
I also know that many people will disagree with what I write here, as that is the nature of how we work ;)
I'm drifting off-topic here, but in short, most of your logic should probably finish up in your models. Your models, well, model your application in terms of data and how that data changes. It's therefore natural that this is where a lot of logic is placed. Your controllers only serve to transport information between the models and your end user (which just so happens, to be via the view).
Quite a long-winded way to say "I agree with John", eh? ;)