通过视图模型或视图助手访问视图中的数据库是否会破坏 MVC 范式?
我相信以下所有内容都打破了 MVC 范式,但想仔细检查是否是这种情况。在所有情况下,视图都是直接访问数据,而不是传入数据。根据我对 MVC 的理解,它永远不应该这样做。控制器应该获取渲染视图所需的所有数据,以免直接耦合视图和模型。我的理解正确吗?
通过视图助手访问数据库
# 在 app/helpers/view_helper.hrb 中 def some_view_helper(person_id) @person = Person.find(person_id) 结尾
通过视图助手访问另一个 Web 服务器
# 在 app/helpers/view_helper.hrb 中 def another_view_helper(person_id) # 通过网络发出 http 请求以获取 json @json = WebService.get_person(person_id) 结尾
通过视图模型访问数据库
# 在 apps/controller/person_controller.rb 中 定义显示 @person = Person.find(params[:id]) @page_model = PageModel.new(@person) 结尾 #in app/views/persons/show.html.erb <% @page_model.friends.each |friend| %> ... <%结束%> #in app/models/person.rb 类 Person < ActiveRecord::基础 有很多:朋友 结尾 #在应用程序/模型/page_models/page_model.rb def 初始化(人) @人=人 结尾 定义朋友 @person.朋友 结尾
通过视图模型访问Web服务器获取数据
# 在 apps/controller/person_controller.rb 中 定义显示 @person = Person.find(params[:id]) @page_model = PageModel.new(@person) 结尾 #in app/views/persons/show.html.erb <% @page_model.friends.each |friend| %> ... <%结束%> #在应用程序/模型/page_models/page_model.rb def 初始化(人) @人=人 结尾 定义朋友 WebService.get_friends_for_person(person_id) 结尾
I believe all of the following break the MVC paradigm but wanted to double check if this was the case. In all cases the view is directly accessing data rather than having the data being passed in. From my understanding of MVC, it should never do that. The controller should get all the data that is necessary to render the view as to not couple the view and model directly. Is my understanding correct?
Accessing the database through a view helper
# in app/helpers/view_helper.hrb def some_view_helper(person_id) @person = Person.find(person_id) end
Accessing another web server through a view helper
# in app/helpers/view_helper.hrb def another_view_helper(person_id) # makes http request over the wire to get json back @json = WebService.get_person(person_id) end
Accessing the database through a view model
# in apps/controller/person_controller.rb def show @person = Person.find(params[:id]) @page_model = PageModel.new(@person) end #in app/views/persons/show.html.erb <% @page_model.friends.each do |friend| %> ... <% end %> #in app/models/person.rb class Person < ActiveRecord::Base has_many :friends end #in app/models/page_models/page_model.rb def initialize(person) @person = person end def friends @person.friends end
Accessing web server to get data through a view model
# in apps/controller/person_controller.rb def show @person = Person.find(params[:id]) @page_model = PageModel.new(@person) end #in app/views/persons/show.html.erb <% @page_model.friends.each do |friend| %> ... <% end %> #in app/models/page_models/page_model.rb def initialize(person) @person = person end def friends WebService.get_friends_for_person(person_id) end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 1 和 2,您只需在控制器中设置一个实例变量 (
@person
)。对于 3,你的视图代码还不错,但是为什么要有一个单独的页面模型呢?您还可以在控制器中预先加载好友:
示例 4 有点糟糕,因为您正在视图中进行外部 Web 服务调用。不要那样做。
本文提供了一个很好的示例,展示了理想的干净视图的样子:http://warpspire。 com/posts/mustache-style-erb/
For 1 and 2, you could just set an instance variable (
@person
) in the controller.For 3, your view code isn't so bad, but why have a separate page model? You can also load the friends up front in the controller:
Example 4 is a bit worse, since you're doing external web service calls in a view. Don't do that.
This article has a good example of what an ideal clean view would look like: http://warpspire.com/posts/mustache-style-erb/