E-mailing in MVC, the final frontier...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

E-mailing in MVC, the final frontier...

Post by Luke »

I have slowly but surely found the optimal placement of every aspect of my application logic within MVC (optimal to my preferences anyway). There have been many obstacles that I've had a hard time finding a place I found suitable for, for instance,

I struggled with validation for quite some time, as evidenced by the 900 posts on this board I have posted about validation, and finally ended up writing (with the help of arborint and a few others) a rockin' awesome filter/validation chaining library that solves the problem really elegantly. I am completely satisfied with how I handle forms now... FINALLY!

I then struggled with a constant tendency to design really fat controllers, and almost non-existent models. I didn't even realize I was doing it until I started working with other developers and found that their controllers had almost no code, the majority of logic lying in their models. I have since rethought MVC and realized that I was interpreting the purpose of the controller completely wrong. So, I have finally adopted a more fat-model skinny controller approach that has worked out great.

Now it appears I'm finally approaching the final frontier. This is an area I have never had a solution I liked, instead relying on "good enough" solutions for e-mailing in MVC. E-mailing is an area that reminds me somewhat of form-handling. Like form-handling, e-mailing may involve several or all tiers of MVC. It generally requires at least one model (user, product, etc), and it needs a view (the e-mail template). What I'm unsure about is how to wrap these two together. A controller?

The question of how to deal with E-mailing becomes even more hard for me to answer when it comes to applications that send a lot of e-mail notifications and the like. In these types of applications, it just seems impractical to put this code in the controller, creating swift objects over and over. There has to be a better way.

Where do you put e-mail notification code? Model? Controller? Some kind of e-mailer factory class? I've considered all of these and nothing seems right. :(
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: E-mailing in MVC, the final frontier...

Post by Christopher »

I think it depends on how you want to look at the Emails. Are they another View? Or do you consider them to be more like logging -- a cross-cutting concern?
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: E-mailing in MVC, the final frontier...

Post by Chris Corbyn »

If it's happening on a page request then a simple command object, or a job if you're queuing mail with an email-specific view (ensures absolute URLs, possibly even shortened lines etc) works for me.

Code: Select all

class ContactController {
  public function sendMessage() {
    // ... snip ...
 
    $this->_sendEmail($from, $subject, $body);
    
    // ... snip ...
  }
 
  private function _sendEmail() {
    $email = new EmailJob('some/view/template.php');
    $email->view->setVars(array(
      ...
    ));
    MyRegistry::lookup('job_queue')->addJob($email);
  }
}
To all intent and purpose, the email view is just like any other view. It just needs sending ;)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: E-mailing in MVC, the final frontier...

Post by Eran »

I usually create an Email model that contains the notification logic of the particular application, and abstracts concrete calls to a mail wrapper (such as Zend_Mail or Swift). In the controller I call public methods of that model that contain the logic for sending the notifications.

Sometimes I replace this model with a generic logger that optionally sends Emails as well.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: E-mailing in MVC, the final frontier...

Post by alex.barylski »

There have been many obstacles that I've had a hard time finding a place I found suitable for, for instance,
Anyone who is pedantic about design always will. ;)

I still question daily whether X goes in my model or controller...whether Y is validation code or application code, etc. I think the real problem is that books on patterns are nothing but guidelines, but then you read conflicting remarks in another book that they are "blueprints" which are a little more conrete.
I then struggled with a constant tendency to design really fat controllers, and almost non-existent models. I didn't even realize I was doing it until I started working with other developers and found that their controllers had almost no code, the majority of logic lying in their models.
I've heard and read arguments favouring both...I think it really depends on your requirements and personal development style...

For instance, the code you write: Do you want to re-use internally in the project at hand or in future projects as well?

By nature controllers are usually more more "applicaiton" specific, so it might make sense to make the models more light weight so they bend and fit into other projects, not-nessecarily your own framework but the likes of Joomla, etc. Does your validation go in the model or controller? Well in the case where you might want to reuse a model in a third party a framework which already performs validation, you would introduce duplication and occassionally conflict of concerns and get a nasty error.
I have since rethought MVC and realized that I was interpreting the purpose of the controller completely wrong. So, I have finally adopted a more fat-model skinny controller approach that has worked out great
As long as it works for you is what is most important...I disagree with any statement either direction being "wrong" though.
This is an area I have never had a solution I liked, instead relying on "good enough" solutions for e-mailing in MVC. E-mailing is an area that reminds me somewhat of form-handling. Like form-handling, e-mailing may involve several or all tiers of MVC. It generally requires at least one model (user, product, etc), and it needs a view (the e-mail template). What I'm unsure about is how to wrap these two together. A controller?
If you have the requirement to display a view/template. Use a controller.
If you have the requirement to utilize a model and perfprm some action there after (redirect, forward, show errors). Use a controller.

About the only time I don't use MVC is trivial transaction scripts, mostly to quickly test a design. Or in a really high performance cron script which is executed in the background, needs minimal resources, no authentication and returns nothing to the end user. Otherwise I use MVC.
Where do you put e-mail notification code? Model? Controller? Some kind of e-mailer factory class? I've considered all of these and nothing seems right.
I have a notification object and a swift helper object which wraps connections, messages, etc. Notification obect has a send() method that accepts some paramters, prepares a notification and delegates the rest to Email. Because notifications are usually automated responses based on user "events" they are treated like any other action within an application.

If I were to queue the messages up, at that point I would follow a transaction script approach, which does nothing but connect, retreive and send.

Cheers,
Alex
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: E-mailing in MVC, the final frontier...

Post by Christopher »

PCSpectra wrote:As long as it works for you is what is most important...I disagree with any statement either direction being "wrong" though.
If it is true that there is no wrong then what would be the point of asking questions or giving answers or engaging other programmers at all. Certainly if you misunderstand a problem or a pattern then you are quite probably doing it wrong (and Ninja admitted). If there is no wrong then there is no reason to learn and improve. Programmers, especially solo developers or very small teams, often think they have now developed a good solution when they have probably gone from a terrible solution to a wrong one. They have yet to discover a good solution. How do I know this to be true? -- first hand experience. ;)
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: E-mailing in MVC, the final frontier...

Post by alex.barylski »

Meh...that boils down to personal experience I guess...

I do not subscribe to the theory that many developers solve problems better than solo or small teams...in fact it goes against common knowledge that most great ideas are spawned and grown by small teams or individuals with dedication and more insight into a problem domain than the general public, so I'm not sure what you meant by that.

I also don't think Ninja is totally clueless as to what his requirements are, it sounds like he has experienced both the fat and thin controller/model approach and made the realization that one works in some instances and the other works better in other instances, how and where and why I believe is what he is asking, not which one is better and why.

There are instances where a thin model should be favoured over a thin controller and visa versa. ANyone that argued one or the other is just blowing hot air. How do I know this? Personal experience. ;)

Cheers,
Alex
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: E-mailing in MVC, the final frontier...

Post by josh »

Can't you have a 2 tiered ( templated ) approach to email building, within the model layer? I'm with arborint - cross cutting concern, perhaps you could use an event based design pattern to decouple stuff from the notification tier, like observer's with message channels or something.. If you used a 2 step view to build the email you could do some basic HTML generation in a different class / method in the model, and have it wrapped in an outter template before it gets sent off in some other tier
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: E-mailing in MVC, the final frontier...

Post by Christopher »

PCSpectra wrote:Meh...that boils down to personal experience I guess...
Not just experience. Like everything else in life it boils down to learning how to do it in a good way. One can have lots of experience and still not be doing things well.
PCSpectra wrote:I do not subscribe to the theory that many developers solve problems better than solo or small teams...in fact it goes against common knowledge that most great ideas are spawned and grown by small teams or individuals with dedication and more insight into a problem domain than the general public, so I'm not sure what you meant by that.
Yeah I know you don't subscribe to that idea. But the truth is that maybe great ideas are spawned by individuals, but they certainly aren't built by individuals. Significant software applications are just not built by "an insightful guy" somewhere.
PCSpectra wrote:I also don't think Ninja is totally clueless as to what his requirements are, it sounds like he has experienced both the fat and thin controller/model approach and made the realization that one works in some instances and the other works better in other instances, how and where and why I believe is what he is asking, not which one is better and why.
It was Ninja who said he was initially wrong in building fat controllers. Building thin controllers is not intuitive. You need to get a feel for not only were the MVC separations should be, but why they should be where they should be.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: E-mailing in MVC, the final frontier...

Post by alex.barylski »

Yeah I know you don't subscribe to that idea. But the truth is that maybe great ideas are spawned by individuals, but they certainly aren't built by individuals. Significant software applications are just not built by "an insightful guy" somewhere.
Depends on your definition of significant.

All of the tools I use are developed by one or a few individuals at most, other than the operating system, which is of course a momumental task. That has nothing to do with innovative capacity of an individual, but just plain old "work". While a single man could never move a mountain, it's usually only a few who make exeuctive decisions and make a project like that project move forward. The rest of the people involved are nothing but lemmings.

Cheers,
Alex
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: E-mailing in MVC, the final frontier...

Post by Christopher »

PCSpectra wrote:Depends on your definition of significant.
Yeah, yeah ... everything is just your opinion and perspective.
PCSpectra wrote:All of the tools I use are developed by one or a few individuals at most...
But none of those tools are "great ideas" -- the are all just "yet another" of very common well understood ideas. And I bet many of them are actually developed larger teams than you are admitting ... PHP for example.
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: E-mailing in MVC, the final frontier...

Post by Eran »

I generally agree with arborint on this. Just because something works doesn't mean it's well-built, and there are definitely bad practices for software development.
By nature controllers are usually more more "application" specific, so it might make sense to make the models more light weight so they bend and fit into other projects, not-necessarily your own framework but the likes of Joomla, etc. Does your validation go in the model or controller? Well in the case where you might want to reuse a model in a third party a framework which already performs validation, you would introduce duplication and occasionally conflict of concerns and get a nasty error.
Models are application specific almost as much as controllers, since they encapsulate the domain logic of the particular application. If you have something that is more generic than the current application needs, it's probably best to move it into a library class or a helper. The point for thin controllers is that logic inside controllers is hard to reuse in the same application (outside of other controllers that is), and models were built just for that purpose (can be used everywhere). Also, putting more logic in the models make it easier to abstract it from the data source (which the controller knows about). The model just expects it in a certain format, it doesn't care where it got it from.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: E-mailing in MVC, the final frontier...

Post by Christopher »

As usual, pytrin distills the subject to its essence. Apparently thin controller aren't thinner than good controllers -- they are good controllers. That is because the domain specific stuff has been moved into the Models where it belongs. Very interesting point and way of thinking about it. I have a little more insight into MVC than I did 5 minutes ago.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: E-mailing in MVC, the final frontier...

Post by alex.barylski »

Models are application specific almost as much as controllers
Not even close dude....sorry...

The driving force behind MVC/MVP was the fact that in multiple Windowed applications is was possible to represent a single data model using multiple views, think an accounts table and showing the data as a list, chart and graph.

With the introduction of RPC and then Web services, the idea of re-using a single data model in multiple applications all on different platforms, different views and thus different controllers "is and should be" your major influence when building applications for the web.

Like I said, unless you guys are stuck in a bubble developing toys and never have a need to share data among other applications, then go a head and make your controllers single line invocations and your models fat as hell...but when you wrap that model with a web service and invoke it from a desktop application to render some results...you might find yourself refactoring the models. I've been there.

You can read all the Fowler books in the world about what should go into a model and live by those doctrines but one thing I can promise you, is that Fowler and all is wisdom will never explain the problem I am solving in a manner as clear as I understand it. It's ultimately that simple. No offense, but theory is as good, as a great idea that is never executed.

Cheers,
Alex
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: E-mailing in MVC, the final frontier...

Post by josh »

Your argument rings of "I have a clear vision of an idea therefore it is true", magic spaghetti monster argument. Can you explain in terms I can understand how a transaction script is more re-usable than a domain model? Eh, what does Fowler have to do with this? No ones playing the zealot card but you here.
Post Reply