开发者

Cross controller function conventions for MVC web?

开发者 https://www.devze.com 2023-01-08 16:27 出处:网络
I have a question about the best practices of placing certain functions in controllers in an MVC web framework.

I have a question about the best practices of placing certain functions in controllers in an MVC web framework.

Lets say I ha开发者_如何学Pythonve 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号