Rails:瘦控制器 vs. 胖模型,还是我应该让我的控制器厌食? [英] Rails: Skinny Controller vs. Fat Model, or should I make my Controller Anorexic?

查看:18
本文介绍了Rails:瘦控制器 vs. 胖模型,还是我应该让我的控制器厌食?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前有人回答过类似的问题——例如:

I know similar questions have been answered before - such as:

  • 逻辑应该去哪里
  • 在哪里完成某些任务等

但我有一个更具体的问题 - 我应该如何理解这个公理:保持你的控制器瘦,让你的模型变胖!

But I have a more specific question - How far should I take this axiom: keep your controller skinny, make your model fat!

这是一个例子:

例如,假设我有多个验证数据来源.一个很好的例子是 VIN 号码 -我可以对照制造商数据源、DMV 的数据源以及我的本地数据库进行验证 - 以查看我的记录.所以我有一个名为 Vin 和 vins_controller 的模型.在模型内部,我有 5 种方法:

For instance let's say I have multiple source of verification data. A good example would be a VIN number - I can verify it against, manufacturers data source, DMV's data source, also my local databases - to see what I have on record. So I have a model called Vin and vins_controller. Inside the model I have 5 methods:

  • check_against_local_db
  • check_against_dmv
  • check_against_car_maker_1
  • check_against_car_maker_2 等

在我的控制器中与 REST 保持一致,在操作中显示 - 我有一个简单的 case 语句,它查看 params[:source],并基于指定的源 - 将调用特定的检查方法.

In my controller keeping with the REST, in action show - I have a simple case statement which looks at the params[:source], and based on source specified - will call specific check method.

现在问题来了:我应该保留控制在控制器中调用哪个数据源的逻辑,还是应该将其移动到模型中,然后在控制器中执行诸如 check_vin(source, vin) 之类的操作?

Now here is the question: Should I leave the logic that governs which data source to call in controller or should I move it to model and then in controller just do something like check_vin(source, vin)?

我应该让我的控制器厌食吗?

Should I make my controller anorexic?

编辑

我正在将其转换为@jay-godse 的官方回答(谢谢 - 当时这是一个很好的答案).自 2010 年以来,情况发生了很大变化,这个问题仍然得到了一些看法 - 所以希望这会为一些人指明正确的方向并帮助他们正确组织他们的代码.

I'm switching this to official answer from @jay-godse ( thank you - at the time it was a good answer). Things changed a lot since 2010 and this question still gets some views - so hopefully this will point some people in the right direction and help them organize their code properly.

Trailblazer gem 很好地解决了问题中提出的问题.

Trailblazer gem addresses problems brought up in the question really well.

推荐答案

您应该尝试一下 Trailblazer.这是一个基于 Rails 的瘦框架(实际上,它与所有 Ruby 框架一起运行),它引入了服务层、表单对象、持久性和域代码之间的间接性等等.

You should give Trailblazer a go. This is a thin framework on top of Rails (actually, it runs with all Ruby frameworks), it introduces a service layer, form objects, indirection between persistence and domain code, and so on.

让您的所有软件组件保持精简真的很有帮助,因为它为您提供了更多的抽象层,让您更容易找出放置什么的位置.

It really helps to keep all your software components skinny as it gives you more abstraction layers that make it so much easier to figure out where to put what.

例如,当您有一个表单对象时,为什么要将验证逻辑压入控制器和模型中?

For example, why would you press your validation logic into controller and model when you have a form object for that?

这篇关于Rails:瘦控制器 vs. 胖模型,还是我应该让我的控制器厌食?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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