views:

110

answers:

4

I have a User class in the model and need to send a password reminder email.

  • Does the controller instantiate a User and pass it (or values from it, rather) to a Email class in the view? In which case, would the controller then do the actual sending of the email?
  • Or would the controller call User::sendEmail() and there not even be a view for the email (and the User do the sending)?
  • Or is there another model class for Email which handles the sending, and it is given to the view Email?

Or am I completely lost? ;-)

Thanks for any help you may be able to give me!

+4  A: 

Unless you are building a messaging system, emails would rather be a resource than part of the core domain. You can treat them as any other kind of resource such as database access, logging and so on.

Personally, I'd abstract it as a MessagingService to avoid too tight a coupling to emails specifically - that would allow you to send off messages along other channels in the future, if relevant. The controller would require an injected MessagingService and send off the message.

The only reason to involve a user object would be if you need values from the user to populate the contents of the email, but that is basically just a transformation of data.

Mark Seemann
+1  A: 

If you really want to break things down into MVC...building off of what Mark Seemann said.

MessageController -> does the sending of the message/email MessageModel -> stores the info for the message/email No View needed.

I would just pass the User information directly to the MessageModel. There's non reason for the MessageModel to be passed the User object directly.

milesmeow
Controllers and Models should never be responsible for composing or sending output. An email is just an alternative output method.
Atli
A: 

I would say:

  1. Controllers do stuff

  2. Views display stuff

  3. It should go in a controller

EDIT: As milesmeow said, it would make sense to create a controller for messaging to allow for expansion. Maybe you'll want to be able to send your notices via Twitter, IM, SMS, or some other medium in the future. It pays to build for the future.

redwall_hp
As you say; Views display stuff. What is an email if not an alternative output method? Controllers and Models should never be responsible for composing or sending output.
Atli
A: 

As I see it, an Email would belong to the display layer of a framework, which in the case of the MVC concept is the View. Emails are just another display option for the output of the request, after all.

Neither Models nor Controllers should be used to create or display output. It's the whole point of using a design pattern like MVC.

Edit To clarify; the Controller would trigger the sending of the 'output', which the View would compose and send - whether as an email, HTML or whatever else that specific View was designed for -, using data collected from a Model.

Atli
This is how I have done it.The controller creates a Message (which is a View), adds it to a Messenger (which is an external library class, but will be a controller if necessary), and then calls send() on the Messenger. So there's no display logic in the controller, and no action logic in the view; correct? The model doesn't come into it other than that some of its data is fed to the Message by the controller.Is that what you meant?
Sam
Yes, that sounds about right. Although, the MVC concept would allow the View to interact with the Model directly, rather than through the Controller. By having the Controller act as a mediator between the View and the Model, you turn it into something more like a traditional Tier 3 architecture. *(See the diagram in the Wikipedia article; `http://en.wikipedia.org/wiki/Model-view-controller`)*
Atli