在轨使用的视图模型? [英] Are view models used in rails?

查看:115
本文介绍了在轨使用的视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始开发Ruby on Rails的一个小应用程序,还有许多问题出现。我应该说,我有1年左右的ASP.NET MVC的经验和在家里与模型视图和控制器感觉。我一直在使用视图模型广泛(与AutoMapper的帮助下),现在想知道如果视图模型在轨训练营同样使用。

I'm starting to develop a small application in ruby on rails and many questions arise. I should say that I have about 1 year of experience with ASP.NET MVC and feel at home with models views and controllers. I've been using view models extensively (with the help of AutoMapper) and now wondering if view models are used similarly in rails camp.

从各种实例(主要是轨道蒙上)我收集了,这是常见要么就在您的视图(这是在ASP.NET MVC时皱起了眉头)合并多个模型的数据,或者使用模型来虚拟属性获得失踪的数据。

From various examples (rails casts mainly) I've gathered that it is common to either combine data from multiple models right in your view (which is frowned upon in ASP.NET MVC), or to use virtual attributes on models to obtain "missing" data.

我知道,UI需求,例如不应该有模型中的密码确认属性后的商业模式不应该模仿,这应该是一个视图模型属性。

I know that business model should not be modelled after UI needs, for example there should not be a 'password confirm' property in your model, this should be a view model property.

Rails的虚拟财产似乎违反这个原则。

Rails virtual properties seem to violate this principle.

它是如何在轨道做了什么?

How is it done in rails?

感谢。

推荐答案

如果我理解正确的视图模型的概念,这是一个概念,这不是Rails中立竿见影,但也不是禁止/皱起了眉头,或以其他方式不允许。所以你自由的抽象水平是合适的工作,有没有具体的要求,对于一个完美的一对一映射模型表。

If I understand the notion of View Model correctly, it's a concept that's not immediately obvious in Rails, but neither is it forbidden/frowned-on or otherwise not allowed. There's no specific requirement for a perfect one-to-one mapping of models to tables so you're free to work at the level of abstraction that's appropriate.

我不认为在一个视图引用多个模型被认为是特别糟糕的(如果是的话,我有需要一点返工部分地区),虽然从一个单一的视图更新多个模型可以得到一个有点棘手。

I don't think referencing multiple models in a view is considered particularly bad (if it is, then I have some areas that need a little rework) although updating multiple models from a single view can get a bit tricky.

总之,在Rails的,我认为我们在谈论什么似乎是大多被称为presenter的格局。有些引用(谷歌Rails的presenter模式超过你可能想要或需要):

Anyway, in Rails I think we're talking about what seems to be mostly referred to as the "Presenter" pattern. Some references (Google "Rails Presenter Pattern" for more than you probably want or need):

  • http://blog.jayfields.com/2007/03/rails-presenter-pattern.html
  • http://gilesbowkett.blogspot.com/2007/10/my-version-of-rails-presenters.html
  • http://blog.jayfields.com/2007/10/extending-rails.html (mentions that DHH used presenters in Highrise)

这篇关于在轨使用的视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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