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

查看:197
本文介绍了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等。

  • check_against_local_db
  • check_against_dmv
  • check_against_car_maker_1
  • check_against_car_maker_2, etc.

,in action show - 我有一个简单的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.

现在这里是一个问题:
我应该保留控制哪个数据源在控制器中调用的逻辑,还是应该移动到模型然后在控制器

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?

EDIT

我将此切换为来自@ 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 < a> a go。这是一个在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天全站免登陆