模型或控制器是否应该负责发送电子邮件?
在 MVC Web 应用程序中,我经常发送电子邮件。
我通常在控制器中执行此操作,因为我从控制器加载所有视图(包括电子邮件视图)。
然而,现在我有了一些从模型发送电子邮件的代码。
电子邮件通常从哪一层发送?有关系吗? 需要保持一致吗?
In a MVC web application, I often send emails.
I usually do it in the controller, as I load all my views from the controller (including email views).
Now, however, I have some code where the email sends from the model.
Which tier is email generally sent from? Does it matter? Does it need to be consistent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理想情况下,控制器应该像将视图连接到模型的操作符。这要么属于模型层,要么属于服务层。
我认为,只有当您有一个单独负责发送电子邮件的模型对象时,它才属于模型层。您不想混合表示和逻辑,这就是模型-视图-控制器中关注点分离的全部要点。
这种类型的逻辑应该驻留在服务层中。然后,您可以使用依赖项注入将服务注入控制器并调用 EmailSenderService.sendEmail();
A controller should ideally be like an operator that connects a view to a model. This either belongs in the model or service layer.
I would argue that this belongs in the Model layer only if you have a model object that is solely responsible for sending e-mails. You don't want to comingle presentation and logic, that's the whole point of separation of concerns in Model-View-Controller.
This type of logic should reside in a service layer. You could then use dependency injection to inject the service into the controller and call EmailSenderService.sendEmail();