Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the model or controller be responsible for sending emails?

Tags:

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?

like image 444
alex Avatar asked Sep 22 '10 00:09

alex


People also ask

How does MVC know which controller to use?

Also, MVC relies heavily on reflection, which allows you to inspect types at runtime using strings. Reflection is used in many programming frameworks.

What are controllers in programming?

A controller, in a computing context, is a hardware device or a software program that manages or directs the flow of data between two entities. In computing, controllers may be cards, microchips or separate hardware devices for the control of a peripheral device.

What does model-view-controller represent in an MVC application?

In object-oriented programming development, model-view-controller (MVC) is the name of a methodology or design pattern for successfully and efficiently relating the user interface to underlying data models.


1 Answers

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();

like image 135
Visionary Software Solutions Avatar answered Oct 02 '22 22:10

Visionary Software Solutions