By Peter Bell

Proposed New Years Resolution: Write Re-usable Models

Sean has mentioned this before and I think I locked a pretty good test for putting the right functionality into your Controller (hint: almost none!), but I still see a lot of applications with business logic in the controller. It’s a good anti-pattern to learn to avoid.

The best patterns (and anti-patterns) are driven by use cases. The use case driving this anti pattern is that as we move into 2007 more and more applications should be able to expose their model directly for non-page based requests (think Flex, AJAX and/or web service calls). Given that you want to keep your application DRY (check out the PDF), it is important that you don’t put any business logic in your HTML controller unless the logic is HTML page specific. Check out Sean's article and the two simple questions for more on how to avoid this anti-pattern.

Of course, if you have an application that you know will never need to expose the model to anything other than HTML page requests, it doesn’t really matter if you put some business logic in your controller, but if you ever wanted to change frameworks (say between MG and M2) the less you put in the controller, the better.

Comments
"it doesn’t really matter if you put some business logic in your controller"

I would add to that, "if for some reason cohesion isn't important to you."
# Posted By Sam | 1/3/07 11:23 AM
Agreed. But sometimes it isn't! For all my OO proclivities, I still write views regularly that contain queries within them - typically for simple weblets with minimal complexity and they code quick and work just fine. No point writing more maintainable code if you KNOW the client doesn't have budget to pay for it.

Case in point: I spend a good chunk of my time doing 2-4 hour projects to add a new feature to an existing site I've built. Typically new db table, object, admin list, add, edit and delete for 1-2 objects and maybe 2-3 front end views within the existing content management system. Until I'm done with my OO generator I simply can't spec, code, deploy, test and deliver an OO project with a couple of custom objects and a total of 10-11 screens in a couple of hours but I can do that easily the "old way".

And the clients? They get what they want - just enough engineering at the lowest possible price. It is that work (and the hosting revenue) that pays for me to be able to work on all the OO and app gen patterns the rest of the time while still making payroll!
# Posted By Peter Bell | 1/3/07 12:06 PM
I agree with you Peter in doing "the old fashion" top down for small weblets. However, if you employ the use of a framework, it could accelerate it even more with even more maintainability. Specially when a Client does not have the budget and you don't have the time. I have found that by just creating events on my coldbox handlers and just calling them from the views, I create simple weblets. My view is separated and my event is separate, nice decoupling. I am still using my controller for logic, which in this case is what you are looking for, but you are going an extra step of separating it from the view. And the good thing, that the logic in the event can be easily ported to another framework. (Only ColdBox :) though) Since it does not rely on XML Dialect. Was that a pitch or what!!
# Posted By Luis Majano | 1/3/07 10:10 PM
Hi Luis,

Agreed 100% (and anyone who hasn't - check out ColdBox - do so - it seems cool to me!!!). I'm working up my own OO framework which makes all of the parts of the process quicker, but until that is in place, I still throw together simple projects with a simple architecture and it's important to remind people that not EVERYTHING has to be OO!
# Posted By Peter Bell | 1/4/07 4:17 AM
"I still write views regularly that contain queries within them"

Well, I do that too. =) If there is not much use for the MVC tool, then there simply isn't much use for it. I don't think its the end-all architecture to everything. Sometimes using it is like taking your RV to the grocery store instead of your car. It will get the job done, but it's a little too much.

But, that can still be cohesive (I suppose there are degrees of cohesion, rather than being "either-or"). I was thinking though, that if you are taking the time to (try to) build a well-architected application, then you may as well keep your classes cohesive. With that said though, I often thought of my models as dumb - they know about the data, but nothing more. Therefore, my controllers "controlled" the business logic. But, having them cfc-based, I could still use them for non-html front-ends. However, it's not the models that were dumb - it was just me - and I guess I was just calling everything by someone else's name.
# Posted By Sam | 1/4/07 4:41 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.