MVC Web 的跨控制器功能约定?
我有一个关于在 MVC Web 框架的控制器中放置某些功能的最佳实践的问题。
假设我有一个用户 &标签控制器。我有一个名为 addTag_to_user() 的函数/方法。
将其放置在用户或标签控制器中的约定或最佳实践是什么?
尽管我个人更倾向于将其放置在标签控制器中,因为我的用户控制器可能充满其他功能,因为它是一个关键元素(例如)。因此,将其放置在标签控制器中可以提高用户控制器的可读性。
编辑:也许将处理两个模型的函数放在哪里并不重要。我想看看其他人的约定是什么。
I have a question about the best practices of placing certain functions in controllers in an MVC web framework.
Lets say I have a User & Tags controller. I have a function/method called addTag_to_user().
What is the conventions or best practices for placing this in either the Users or Tags controller?
Though personally I am more inclined to place it in the tags controller as my Users controller may be full of other functions as it is a key element (for example). Thus placing it in the Tags controller improves the readability of the User controller.
Edit: Maybe it doesn't really matter where you place functions that deals with two models. I want to see what other people's conventions are.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
控制器对象可以保存操纵模型域的行为。然而,MVC 通常每个域都有一个控制器,听起来“用户”和“标签”属于同一个域。
根据信息专家原则,谁拥有履行这一职责的信息?标签不知道用户。合乎逻辑的答案是用户对其标签负责。所以你的用户类将保留 addTag() 行为。
The Controller object can hold behaviour which manipulates the Model domain. However, MVC normally has one controller for each domain, and it sounds like "User" and "Tag" belong in the same domain.
With the Information Expert Principle, Who has the information to fulfil this responsibility? A Tag has no idea about a User. The logical answer is that the User is responsible for his/her tag's. So your User Class would hold addTag() behaviour.