开发者

Sending email updates: model or observer?

I have an Event model, which stores an event feed for each user. I also need to email the updates to users which have enabled email notifications in their profile.

From an architectural point of view, which one is better?

  1. call the mailer i开发者_开发知识库n an after_create method in the model, since it's a part of the business logic;
  2. call the mailer in an after_create method in an observer, since it doesn't really operate on the model and it also depends on the user's settings.


I'd go with an observer. My reason for this is that sending a notification email isn't an essential part of the business logic of an event (put simply, events don't send notifications). Other than that, as Damien has already said, it's a matter of separation of concerns.

Please, see the discussion here, as well.


There's no "magic" answer to that as it's subjective.
Some developers will prefer to do that directly in the model. Others will do it in an observer.
What you do is completely up to you. Both solutions are valid.

I personnaly prefer to add them in an observer as it allows me to have all the user notifications in the same place and not one in one model and an other in an other model.


The observer approach will be more flexible. If your email setting (addressee or server) changes, or the criteria for notification (daily to weekly or vice versa), it is a simpler matter to change the observer than it is to modify the model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜