在MVC中使用Mail Model是不正确的?

时间:2022-10-13 12:45:29

I have built a model in some of my MVC websites to assist with sending emails, generally I do something like this

我已经在我的一些MVC网站上建立了一个模型来帮助发送电子邮件,通常我做这样的事情

$mail = new Mail_Model;
$mail->to('me@somewhere.com');
$mail->from('you@somewhere.com');
$mail->subject('hello');
$mail->body('hello how are you');
$mail->send();

I know the model is meant to model data - so is what I'm doing a violation of that? Should it be a helper class instead? Something like...

我知道这个模型是为了模拟数据 - 所以我正在违反这个规则吗?它应该是帮手类吗?就像是...

Mail::send('me@somewhere.com', 'you@somewhere.com', 'hello', 'hello how are you');

That second one doesn't read as well to me.. of course I could pass in an array with named keys to make it more readable.

第二个对我来说也不太好。当然我可以传入一个带有命名键的数组,以使其更具可读性。

So is my Mail model violating what a model should be in the MVC paradigm?

那么我的Mail模型是否违反了MVC范例中的模型?

3 个解决方案

#1


1  

since this question is tagged with 'kohana' maybe you already know that kohana has an email helper that does exactly what you're doing here.

因为这个问题用'kohana'标记,也许你已经知道kohana有一个电子邮件助手,它正是你在这里做的。

inherently, my answer is that the code to send an email would fit better as a helper.

本质上,我的答案是发送电子邮件的代码更适合作为帮助者。

#2


1  

The mail isn't really a model, I suspect, unless you're saving it to the database or doing some other complicated processing with it.

我怀疑,邮件并不是真正的模型,除非您将其保存到数据库或使用它进行其他复杂的处理。

IMO, instantiating an object in this case is probably not very 'MVC', and your second example looks (at least to me) more like a line of code that indicates a one-off action with no real business logic repercussions.

IMO,在这种情况下实例化一个对象可能不是非常“MVC”,而你的第二个例子看起来(至少对我来说)更像是一行代码,表示一次性动作而没有真正的业务逻辑反应。

But - don't feel too constrained by the paradigm! Which of those two options do you think is easier to read, and easier for another coder to follow along with later?

但是 - 不要被范式所束缚!您认为这两个选项中的哪一个更容易阅读,并且更容易让其他程序员随后跟进?

#3


0  

Take '_Model' off your first one and that would make perfect sense to me. Implementing mail-sending as a single static function gives you no benefit over just calling mail(), really. you'll end up with the same problems trying to set additional headers, or send HTML email, or add attachments that extra methods on a class can abstract from your calling code.

把'_Model'从你的第一个拿走,这对我来说非常有意义。将邮件发送作为单个静态函数实现,除了仅调用mail()之外,没有任何好处。你最终会遇到同样的问题,试图设置额外的标题,或发送HTML电子邮件,或添加一个类的额外方法可以从你的调用代码中抽象出来的附件。

#1


1  

since this question is tagged with 'kohana' maybe you already know that kohana has an email helper that does exactly what you're doing here.

因为这个问题用'kohana'标记,也许你已经知道kohana有一个电子邮件助手,它正是你在这里做的。

inherently, my answer is that the code to send an email would fit better as a helper.

本质上,我的答案是发送电子邮件的代码更适合作为帮助者。

#2


1  

The mail isn't really a model, I suspect, unless you're saving it to the database or doing some other complicated processing with it.

我怀疑,邮件并不是真正的模型,除非您将其保存到数据库或使用它进行其他复杂的处理。

IMO, instantiating an object in this case is probably not very 'MVC', and your second example looks (at least to me) more like a line of code that indicates a one-off action with no real business logic repercussions.

IMO,在这种情况下实例化一个对象可能不是非常“MVC”,而你的第二个例子看起来(至少对我来说)更像是一行代码,表示一次性动作而没有真正的业务逻辑反应。

But - don't feel too constrained by the paradigm! Which of those two options do you think is easier to read, and easier for another coder to follow along with later?

但是 - 不要被范式所束缚!您认为这两个选项中的哪一个更容易阅读,并且更容易让其他程序员随后跟进?

#3


0  

Take '_Model' off your first one and that would make perfect sense to me. Implementing mail-sending as a single static function gives you no benefit over just calling mail(), really. you'll end up with the same problems trying to set additional headers, or send HTML email, or add attachments that extra methods on a class can abstract from your calling code.

把'_Model'从你的第一个拿走,这对我来说非常有意义。将邮件发送作为单个静态函数实现,除了仅调用mail()之外,没有任何好处。你最终会遇到同样的问题,试图设置额外的标题,或发送HTML电子邮件,或添加一个类的额外方法可以从你的调用代码中抽象出来的附件。