How to separate functionality of Angular directive?

CakePHP: Has anyone ever seen email functionality inside a model of MVC?

  • I am using CakePHP, one of my co-worker has added email functionality inside a model function while writing database queries.  My co-worker argues that models should be fat and so this functionality should go inside model. Has anyone ever seen email functionality inside a model of MVC? Shouldn't email functionality go inside the controller because models are only required to fetch data from data-source?

  • Answer:

    An email is a view, you should respect MVC pattern ; select data to put in the email from the model, send it to the view through controller, render the view as HTML or plain text, and tweak your controller to send the rendered view.

Xavier Laumonier at Quora Visit the source

Was this solution helpful to you?

Other answers

DISCLOSURE: I am a CakePHP core developer, but my answers are my own. They are not shared/reviewed by any CakePHP team member before posting and only represent my personal opinion. Both are considered 'bad design'. I would suggest the use of 'events' in CakePHP. Furthermore, sending emails should be a worker's task and so the event should only push the job to a queue that can later be processed, thus moving email sending to the shell (not controller and definitely not model). Now, because I understand that some applications don't need the most advanced setups, the email could be sent directly from the event. This makes for well organized/structured code that can be easily tested.

Jad Bitar

Email functionality should not go into the model OR the controller. The functionality should be a separate service, an internal application API of sorts which can be called from absolutely wherever - model OR controller. Compartmentalization and independent components are key to the success of any big enterprise system.

Bruno Skvorc

Typically, the model encapsulates the data layer and nothing else. This can be problematic in some cases, e.g, if you have multiple controllers that each need to trigger the same e-mail logic. In small systems, moving that logic into the model itself is potentially fine, but the concept of "fat models" is not likely to scale well, as it breaks encapsulation. I generally encourage such logic to be encapsulated in an adapter (sometimes called a "mediating controller"). Your controllers now only talk to the adapter, which sends the e-mail and then calls into the model.

Neal Kanodia

You are right. Email functionality should be implement in controller. As example if ( false !== doModelInstruction() ) { sendEmail() }. Model is dedicated stricte for database manipulation, nothing more.

Roman Piekarski

It all depends on how you interpret MVC. In some cases it's perfectly fine to have email functionality in the Model. In others you need to have a different approach as suggested. It should never be in the controller, however. At least specifically in terms of CakePHP, it's good to have in the model if you understand the implications and your use and interpretation of MVC is not broken.

Anonymous

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.