ActionMailer最佳实践:是在模型中还是在控制器中调用方法? [英] ActionMailer best practices: Call method in the model or the controller?

查看:66
本文介绍了ActionMailer最佳实践:是在模型中还是在控制器中调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发送电子邮件通常是在对模型进行操作之后调用的,但是电子邮件本身是视图操作.我正在寻找您如何思考要问自己的问题,以确定将操作邮件程序方法调用放在何处.

Sending an email is usually called after an action on a model, but the email itself is a view operation. I'm looking for how you think about what question(s) to ask yourself to determine where to put the action mailer method call.

我看过/使用过它们:

  • 在模型方法中-相关但分离的关注点耦合不好?
  • 在模型的回调中(例如after_save)-根据目前的知识水平,可以做到最好的分离.
  • 在控制器动作中-感觉很不对,但是在某些情况下,这是构造代码的最聪明的方法吗?

如果我想知道如何编程,那么我需要像程序员一样思考,因此,要独自思考一个月,就要花费数月的时间来学习如何思考特定的编程解决方案.谢谢!

If I want to know how to program I need to think like a programmer, so learning how you go about thinking through particular programming solutions is worth months of coding on my own in isolation. Thank you!

推荐答案

最新答案,但我想对这个问题进行合理化:

Late answer, but I want to rationalize on the subject:

通常,在Web应用程序中,您希望发送电子邮件作为对客户的直接反应.或者作为后台任务,以防万一我们正在谈论时事通讯/通知邮件之类的事情.

Usually, in a web app, you want to send emails either as a direct reaction to a client. Or as a background task, in case we're talking about a newsletter/notification mail sort of thing.

该模型基本上是一个数据存储映射器.其逻辑应将数据处理/通信与数据存储处理封装在一起.因此,插入与此无关的逻辑有些棘手,并且在大多数情况下是错误的.让我们举个例子:用户注册一个帐户,并且应该收到一封确认电子邮件.在这种情况下,可以说确认电子邮件是创建新帐户的直接效果.现在,与其在Web应用程序中进行操作,不如在控制台中创建用户.在这种情况下触发回调听起来不对,对吗?因此,抓取回调选项.我们还应该在模型中编写方法吗?好吧,如果这是用户操作/输入的直接影响,那么它应该留在该工作流程中.成功创建用户后,我将其写入控制器中.直接地.无论如何,在要在控制器中调用的模型中复制此逻辑都会增加不必要的模块化,并增加了Action Mailer对Active Record模型的依赖.尝试考虑在许多应用程序上共享模型,其中一些应用程序不希望使用Action Mailer.出于上述原因,我认为邮寄者打电话应该在他们认为有意义的地方,并且通常模型不是那个地方.尝试给我举个例子.

The model is basically a data storage mapper. Its logic should encapsulate data-handling/communication with data storage handling. Therefore, inserting logic which does not relate to it is a bit tricky, and in most cases wrong. Let us take the example: User registers an account and should receive a confirmation email. In this case one could say, the confirmation email is a direct effect of the creation of a new account. Now, instead of doing it in the web app, try to create a user in the console. Sounds wrong to trigger a callback in that case, right? So, callback option scratched. Should we still write the method in the model? Well, if it's a direct effect of a user action/input, then it should stay in that workflow. I would write it in the controller after the user was successfully created. Directly. Replicating this logic in the model to be called in the controller anyways adds unnecessary modularity, and dependency of an Active Record model from Action Mailer. Try to consider sharing the model over many apps, in which some of them don't want Action Mailer for it. For the stated reasons, I'm of the opinion that the mailer calls should be where they make sense, and usually the model is not that place. Try to give me examples where it does make.

这篇关于ActionMailer最佳实践:是在模型中还是在控制器中调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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