mike42780
05/25/2022, 3:51 PMAdam Cameron
I'm assuming since this isn't directly dealing with a database the answer is to do it in the controllerThis is incorrect thinking. The only think a controller is supposed to do is to accept a request from the routing tier, extract values from the request (eg: form, url, cookies etc that the model might need), pass them to the model tier. Then based on the response from the model, decide which view (and return status code, header, etc) to use to transform the model data for the response to the user. For your specific requirement, the latter is the way to go. Whether or not it's done in an event handler called by the
afterSave
event would be situation-specific, I think. But I think what you suggest is a nice approach.
As for how to create the body of the email in an MVC fashion, in other frameworks I'd generally call a subcontroller to handle that, so the email render itself follows an MVC process, but I suspect CFWheels can't handle this, so you might be stuck.mike42780
05/25/2022, 5:07 PMAdam Cameron
:-/
Peter Amiri
05/25/2022, 6:44 PMmike42780
05/25/2022, 7:11 PMAdam Cameron
in a pure sense of MVC, that may make senseThose are weasel words, Peter. You're positioning this like it's not well-defined what the concepts of model / view / controller mean in MVC. It's also not purist or dogmatic to suggest that there's very good reasons to actually follow industry-accepted design patterns. The problem for CFWheels developers is that the design of CFWheels itself is not great, and it works against writing well-crafted, well-designed code, due to its design limitations. These limitations can be seen in the intro documentation, where CFWheels demonstrates it fundamentally doesn't understand MVC: From https://guides.cfwheels.org/cfwheels-guides/introduction/frameworks-and-cfwheels#model-view-controller-mvc
Model: Just another name for the representation of data, usually a database table.That's just wrong. That is not what the model is. Storage considerations like "database tables" and the handling thereof are just a boundary part of the model: stuff needs to be stored, and a DB is a good place to store it, but that just is the very "edge" of the model, and is usually abstracted well away from the actual domain logic part of the model. CFWheels is not helped here by its decision to promote its interpretation of the ActiveRecord pattern (or close to an anti-pattern IMO, but hey) way too far up into the front of the model. This makes it quite hard to write a good business tier in a CFWheels app. That Mike has to hang business logic of an evert that fires around storage considerations demonstrates this.
Controller: The behind-the-scenes guy that's coordinating everything.Also... if not objectively "wrong" per se, but so poorly described that it kinda suggests how you got to "I put in there but user interaction code I keep in the controller." It's not up to CFWheels of CFWheels devs to redefine the fundamentals of MVC. You can put your code wherever you want. Knock yerself out, but don't kid yerself that yer not really doing MVC; you're just "writing some code". The code will likely be badly-coupled and a testing, maintenance, and scalability... challenge but... well. [shrug] I feel for you though, because CFWheels really does make it hard to write good MVC code.
Adam Cameron
it would be great to have some of the controller functions available in the model, such as sendemail, or includepartial,OMG. Neither of those are methods that belong in a controller anyhow!!! The former should be in a mail service class (part of the model), and include_partial_ is a view method! But. Yes. Back to what I said about CFWheels poor design. I realise there is very little overlap in CFWheels of "where things are implemented" and "where things should be implemented". You are experiencing this ATM, Mike. 😞
Peter Amiri
05/25/2022, 7:28 PMseancorfield
seancorfield
seancorfield
seancorfield
Adam Cameron
mike42780
05/25/2022, 8:00 PMAdam Cameron
Adam Cameron
controller("MyEmailController").run(emailArgsHere)
(conceptual pseudo code) in your email method, and then still have a nice MVC separation for the creation of the email content?Adam Cameron
Adam Cameron
seancorfield
include
-ing template files but eventually we moved those into the database and built a little CMS for the business team to manage the content on their own). It is used somewhat as a "library" all over the Model, wherever the business logic determines an email needs to be sent.seancorfield
Adam Cameron